wikipedia Nach
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers). wikipedia Happy number
I rekursive Funktion isHappy
erstellt, auf die wir unsere Nummer, passieren so
auf das Konto unter Glückliche Nr Definition und dass Javascript Uncaught Auslöser Range trow wird: Maximale Call-Stack-Größe überschritten, wenn wir endlos Schleife werden wir setzen Funktion in versuchen ... fangen Block, So ist hier Schluss Code
//Our Recursive Function
function isHappy(number){
var output = [],
sNumber = number.toString();
for (var i = 0, len = sNumber.length; i < len; i += 1) {
output.push(+sNumber.charAt(i));//converting into number
}
var sum=0;
for(var i=0;i<output.length;i++){
sum+=Math.pow(output[i],2);
}
if(sum!==1){
isHappy(sum);
}
}
try{
isHappy(number);//call the function with given number
alert('happy')
}catch(err){
alert('unhappy')
}
Logic ist nicht korrekt. Berechne num mit der Funktion mod (%). – Amit
bitte dieses https://gist.github.com/xk/8918502 –
hier: http://www.w3resource.com/javascript-exercises/javascript-conditional-statements-and-loops-exercise-8.php –