javascript - I'm trying to raise numbers to their consecutive powers and my code isn't working -
https://codepen.io/aholston/pen/zjbrjd
the codepen link has commented code actual instructions in html
otherwise.... have write function takes 2 params(a , b) , takes numbers between 2 params (a-b) , put every number can added consecutive fowers , equal number new array. ex: 89 = 8^1 + 9^2 = 89 or 135 = 1^1 + 3^2 + 5^3 = 135
function sumdigpow(a, b) { // code here var numbers = []; var checknum = []; var finalnum = []; var total = 0; (var = 1; <= b; i++) { if (i >= && <= b) { numbers.push(i); } } (var x = 0; x < numbers.length; x++) { var checknum = numbers[x].tostring().split(''); if (checknum.length == 1) { var = parseint(checknum); finalnum.push(together); } else if (checknum.length > 1) { var = checknum.join(''); var tognumber = parseint(together); (var y = checknum.length; y > 0; y--) { total += math.pow(checknum[y - 1], y); } if (total == tognumber) { finalnum.push(tognumber); } } } return finalnum; }
try this:
function listnum(a, b) { var finalnum = []; (var = a; <= b; i++) { var x = i; var y = i; var tot = 0; j = i.tostring().length; while (y) { tot += math.pow((y%10), j--); y = math.floor(y/10); } if (tot == x) finalnum.push(i); } return finalnum; } console.log(listnum(1, 200));
Comments
Post a Comment