Javascript: Why are my numbers not being raised to the nth power and being pushed to my array? -
i trying write function takes in number , sums range of digits nth power, seems function not "n-thing" each number in array.
here's code:
function sumdigpow(a, b) { var premarray = []; var squaredarray = []; for(var = a; <= b; i++){ premarray.push(i); } for(var j = 0; j < premarray.length; j++){ var squared = ('' + premarray[j]).split('').map(function(v, i){ return math.pow(parseint(v), + 1); }).reduce(function(a , v){ return + v; }, 0); squaredarray.push(squared); } } sumdigpow(5,22);
you pretty on complicated entire thing.
function digpow(n, p){ var total = 0; var digits = (""+n).split(""); (var = 0; i< digits.length; i++) { total += math.pow(digits[i], p+i); } if(total/n % 1 === 0){ return total/n; } return -1; }
here split first number array. mind string times number can produce number if string of number example: "6".
so split it, iterate through , calculate total result, after check if result divided our start number (n) int , have simple solution. hope helps.
Comments
Post a Comment