N
N
Nurtilek Karabekov2020-08-12 12:57:04
JavaScript
Nurtilek Karabekov, 2020-08-12 12:57:04

Why does the If condition in the recursion return undefined?

Checked the ELSE thread. Everything works correctly there. When the isHappy(1) function is called, undefined is returned instead of true.

var isHappy = function(n) {
    if (n === 1) return true 
    else if (n != 1 && n < 10) return false 
    else {
        const array = Array.from(`${n}`);
        const output = array.map(num =>num * num).reduce((acc, curr) => acc + curr);
        isHappy(output);
    }
};

console.log(isHappy(19))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-08-12
@nurtilex

Because to return something, you need to use return.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question