javascript - Check whether Given number is happy or not -
this code check given number happy number. think trying wrong logic.
var num = 5239; var sum = 0; while (num > 0) { sum += math.pow(num % 10, 2); num = math.floor(num / 10); console.log(num) } console.log(sum); if (num == 0) { alert("happy number") } else { alert("not happy number") }
please correct me correct result.
https://jsfiddle.net/g18tsjjx/1/
if understood happy number is, should correct.
$(document).ready(function() { var num = 1; var = 0; (i; < 10000; i++) { num = newnumber(num); } num == 1 ? alert("it's happy number") : alert("it's not happy number"); }); function newnumber(num) { var sum = 0; var temp = 0; while (num != 0) { temp = num % 10; num = (num - temp) / 10; sum += temp * temp; } return sum; }
Comments
Post a Comment