Factorialize a Number - While loop inside ternary operator

Factorialize a Number - While loop inside ternary operator
0
#1

Hi guys! I’m trying to resolve this problem using ES6 and ternary operators. My challenge is to make a one line code solution. The problem is that I’m obtaining this error SyntaxError: unknown: Unexpected token (6:29) pointing to my while loop. Is not possible to use a While loop inside a ternary operator?

Thank you!

Your code so far


function factorialize(num) {

    let count = num; 
    let i = 0;

return num === 0 ? num = 1 : while (i < count) { num *= count } ;

console.log(num);



}

factorialize(5);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number

#2

I don’t think it is the ternary operator itself the issue, consider that if num === 0 is false, you are returning a while loop, and that may not work how you want (it is not correct syntax)

if you just write as below you get an “unexpected token” error

function factorialize(num) {
  return  while (i < count) { num *= count } ;
}

factorialize(5);
1 Like
#3

You’re right @ieahleen! Thanks!!! Now it’s working, but I think that it’s not concise :weary::

 function factorialize(num) {

return num == 0 ? num = 1 : greaterThanZero() ;

    function greaterThanZero() {
        let i = 1, result = 1;
            while (i <= num) { result *= i; i++; }
        return result;
    }   
}

factorialize(5);
#4

I know that lots of people enjoy trying tool solve challenges in a single line, but I really don’t get it. I know that pushing yourself to an arbitrary goal can be fun, but this is often building bad habits on purpose.

#5

Just for future reference, the reason you’re getting a syntax error is that loops are statements, they describe a series of actions, they don’t have a value. So you’re trying to return something that it doesn’t make sense to return: it’s like writing return var; or return if;, the JS parser just sees it as nonsensical.

1 Like
#6

You can still do better

First, you are returning num = 1 , why don’t you directly return 1? There is no need to assign it…

Also, if you want to do it in one line, you could do it recursively (with the function calling itself)

1 Like
#7

Oh! Thank you for your response! I’m programing since 1 year ago, and it’s difficult to me to implement the new syntax from ES6. All my code solutions have a lot of for loops, a lot of variables reasigned… I figured that if I try to be more concise it will be better. But maybe I’m wrong. Where is the balance? :slight_smile:

#8

Oh! Thanks. It’s true!

return num == 0 ? 1...etc

#9

Shorter isn’t always better. It can often be harder to read (and therefore maintain) and may not save you any computing efficiency. Ternaries, for example, aren’t any more efficient than just writing the if/else.

What we really want to focus on is smoothing out the flow of logic. Ternaries are great when they represent the way we think of things.

if (age < 18){
    status = 'minor';
}else{
    status = 'adult';
}

// But this is a simple binary so the following actually reads better
status = age < 18 ? 'minor' : 'adult';

You mentioned a lot of loops. That’s one area where the ES6 material may be helpful. There are a bunch of very helpful array methods that you can leverage. Otherwise, the key is to look at making your logic more streamlined. Ask yourself if you are iterating over the same collection multiple times or writing the same loop for multiple collections. There are opportunities for improvement. If you find yourself assigning variables several times, is there a good way to do all the operations on the variable bat the same time?

Think “streamlined” rather than “short” as the goal.

#10

Thanks for your response @ArielLeslie! I will think on that.