# Basic Algorithm Scripting - Factorialize a Number

Tell us what’s happening:
when i write this in if else it works but when it write this using ternary operator it is not working can someone explain?

``````function factorialize(num) {
num === 0 ? 1 : num * factorialize(num - 1);
}

console.log(factorialize(5));
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188`

Challenge: Basic Algorithm Scripting - Factorialize a Number

Note - a ternary is not a general purpose replacement for an if statement.

Your main issue is that you aren’t returning anything from this function.

``````function factorialize(num) {
let result = num === 0 ? 1 : num * factorialize(num - 1);
return result;
}
``````

or

``````function factorialize(num){
return num === 0 ? 1 : num * factorialize(num - 1);
}

console.log(factorialize(5));
``````

is this Correct??

Does it pass the tests? Its looks right at a glance.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

both of above mentioned codes passed.

Then its right!

this one is different solution for this task can you add this to solutions.

``````function factorialize(num) {
return num === 0 ? 1 : num * factorialize(num - 1);
}

console.log(factorialize(5));
``````

It’s not conceptually different from Solution 2. That list isn’t intended to capture every possible way to write the solution since that would be an infinitely long list.

I got this both are using recursion the only difference is i used ternary operator instead of if else statement.
Thank you!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.