Unicornzz - 11 days ago 6x

Javascript Question

I'm stuck on this question:

https://docs.google.com/document/d/1NNgUZtB5wy89DaROVNMy22Hfjt3gbwlq9qkMszU8Bu8/edit?usp=sharing

^the link

If you don't trust the link, here is the question:

Loops: Lychrel Numbers

Large Program Practice

If we take 47, reverse the digits and add, we get 47+74 = 121, which is a palindromic number. this means the number remains the same when its digits are reversed. Not all numbers produce palindromes so quickly. For example:

349+943 = 1292

1292+2921 = 4213

4213 + 3124 = 7337

So, for the number 349, it took three iterations to arrive at a palindrome.

Although no one has proved it yet, it is thought that some numbers, like 196 never produce a palindrome no matter how many iterations are carried out. Such a number is called a Lychrel Number. Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.

Write a program that reads one integer number and outputs YES if the input is a Lychrel number and NO otherwise. In addition, if an input number is NOT a Lychrel number, your program must output the number of iterations it took to arrive at a palindrome and the final palindromic number. By forcing the user input to be below 10000, you may assume that the number either becomes a palindrome in under 50 iterations or is a Lychrel number.

Sample Program Execution:

Input: 349

Output: NO 3 7337

Input: 196

Output: YES

Input: 89

Output: NO 24 8813200023188

Input: 592

Output YES

This is what I have so far. I don't understand what I need to do to make it work. I am new to Stack Overflow so if I did anything wrong please let me know. Thanks!

`<!doctype html>`

<html>

<head></head>

<body>

Number: <input type='text' id='number' name='number' placeholder='1234'/> </br>

Re-enter the same number: <input type='text' id='number2' name='number2' placeholder='1234'/>

<button onclick='convert()'>Convert</button></br>

<div id='result'></div>

<script>

function convert(){

var originalNum=parseInt(document.getElementById('number').value);

var newNum=parseInt(document.getElementById('number2').value);

var x;

var total=originalNum;

var palindrome=false;

for (x=0; x<50; x++) {

newNum==newNum.split("").reverse().join();

total+=newNum;

if (total==total.split().reverse().join()){

palindrome=true;

break;

}

}

if (palindrome)

document.getElementById('result').innerHTML=("NO");

else

document.getElementById('result').innerHTML=("YES");

}

</script>

</body>

</html>

Answer

Perhaps there's better ways to do it, but this works.

The main issue you were running into was that JavaScript does different things with the `+`

operator depending on the type of a variable. For strings, it concatenates them but for numbers it adds them. This can be especially confusing since the behavior for different operators is inconsistent. i.e. it will evaluate `"5" == 5`

as true (i.e. It converts strings to numbers for `==`

, but not for `+`

)

Similarly for split and join. They have different behaviors when used with no arguments, although they may seem like they are the inverse of each other.

```
function lychrelTest() {
var originalNum=document.getElementById('number').value;
if (isNaN(parseInt(originalNum))) {
alert('"'+originalNum+'" is not a number');
return;
}
var result = document.getElementById('result');
result.innerHTML = "";
var current=originalNum;
var palindrome=false;
var x;
for (x=0; x<50; x++) {
var rev = current.split("").reverse().join("");
var attempt = current + "+" + rev;
current = (parseInt(current) + parseInt(rev)).toString();
result.innerHTML += attempt + "="+ current + "<br />";
if (current == current.split("").reverse().join("")) {
palindrome=true;
break;
}
}
result.innerHTML+=(palindrome?("NO: "+current+" after "+(x+1)+" iterations"):"YES");
}
```

```
<body>
Number: <input type='text' id='number' name='number' placeholder='1234'/>
<button onclick='lychrelTest()'>Test</button></br>
<div id='result'></div>
</body>
```

Source (Stackoverflow)

Comments