Unicornzz Unicornzz - 1 year ago 111
Javascript Question

Lychrel Numbers Javascript?

I'm stuck on this question:
^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>
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>
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++) {
if (total==total.split().reverse().join()){
if (palindrome)


Answer Source

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');

    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("")) {
    result.innerHTML+=(palindrome?("NO: "+current+" after "+(x+1)+" iterations"):"YES");
	Number: <input type='text' id='number' name='number' placeholder='1234'/>
	<button onclick='lychrelTest()'>Test</button></br>
	<div id='result'></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download