JSFiddle - React, Tailwind, and code Playground

by Jessie Lau

HTML

<!-- - create a function, take a num as parameter, return a signle digit; - loop over the number, reduce method to add them up - if the result length more than 2, sum up it again -->

JavaScript

function AdditivePersistence(num) {
    var sum = 0;
    var loop = 0;
    var number = num.toString().split("").map(function (a) {
        return Number(a)
    });

    if (number.length > 1) {
        var res = 0;
        number.forEach(function (a) {
            res += a;
        });

    }
    return loop;
}
console.log(AdditivePersistence(2718));