FAQ: Code Challenges: JavaScript Fundamentals - numImaginaryFriends()

This community-built FAQ covers the “numImaginaryFriends()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise numImaginaryFriends()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello. Why does 33 percent need to be written as 0.33 and not 33% ?

2 Likes

0.33 is a literal which can be used directly in calculations. 33% is an abstract which would need to be parsed and interpreted before it can be evaluated. It would have to be written in string form else the JS interpreter will see it as a number followed by the modulo operator and missing an operand so would raise an error.

33 % 10 === 3

If we wish to output to the display, then,

pct = 0.33;
`${pct * 100}% of voters were undecided.`

Note that % is string character.

3 Likes

Why is

const numImaginaryFriends = totalFriends => Math.round(totalFriends * .33);

correct while

const numImaginaryFriends = totalFriends => { Math.round(totalFriends * .33); }

throws an error?

3 Likes

It’s the curly braces. When using a block like that, there is no implicit return. It must be explicit.

{
    return ...;
};
4 Likes

Ah, OK. Thanks for the quick reply @mtf.

1 Like

Hi,
Can someone explain to me why I can’t write (1/3) instead of .33?

const numImaginaryFriends = totalFriends => Math.round(totalFriends * (1/3))

I end up with an error saying:

Your function should return a whole number ie 1.3 should round down to 1 while 4.9 should round up to 5

 > totalFriends = 8
=> 8
> Math.round(totalFriends * (1/3))
=> 3
 > totalFriends = 7
=> 7
> Math.round(totalFriends * (1/3))
=> 2

It’s not a problem for JS, but may be a problem with the answer the SCT is seeing. Check the instructions and be sure to follow them to the letter.

2 Likes

Are you still getting a result with that syntax? I got the same error message when I called Math.round(totalFriends / 3); , but still got a result in the console. I don’t think the error message you’re getting is actually related to your code, because if you replace (1/3) with .33, you should get the same result without the error message, and pass.

My questions is why this would be the case. Perhaps it’s just good practice to use decimals over other mathematical syntaxes?

Welcome, @bcrave.

The best practice would be to first read carefully all the instructions, and not draw any conclusions. Take for instance the instruction in step 1.

A person’s number of imaginary friends are always 33% of their total friends.

Note that is states, always 33%. This is a literal, and specific value: Thirty-three per-cent. Being an exact value, there is in no estimation needed, or recommended.

33% =>  33 / 100  =>  0.33

All three representations are equivalent values. If we toss in 1 / 3 we will find it is not equivalent to the other three. That is why we would not rewrite the given value as it introduces a margin of error.

In this particular instance we are rounding so that will result in a closest integer value so the tiny amount of error mentioned above would be inconsequential but it doesn’t change the fact we should always use the values given in the instructions.

0.33  <>  1 / 3

That says the two are not equivalent. The decimal approximation of 1/3 is 0.333… with repeating digits going to infinity. The decimal value is not considered an exact number the way the fraction is.

Mathematically we can produce a fraction from any number with a repeating decimal value.

Repeating decimal - Wikipedia

5 Likes

const numImaginaryFriends = (friends) => {
return Math.round(friends * (1 / 3));
}

After looking at the solution I realise I can ommit the () on the variable (becaise it’s only one variable) and I can ommit curly brackets and ‘return’ as well. However, the code seems to be working and rounding as it’s supposed to but I get this error:
Your function should return a whole number ie 1.3 should round down to 1 while 4.9 should round up to 5.
Can anyone tell me why?
Also, I multiplied by (1 / 3)… I suppose this is closer to 33% than 0.33 - correct?

No, not correct. 0.33 IS 33%, whereas, 1/3 is not. 33% is not closer to one-third, as it is only an approximation. 1/3 is EXACTLY one-third. We should not mix these two up.

2 Likes

Yes, of course. Not sure why I read 33% ad 33,333…%.

Any idea why I get the error I posted above?

1 Like

The exercise states that the number of imaginary friends is always 33% of their total friends. That means to use 0.33 as a coefficient.

1 Like

I cannot log the information to the console? code is below, it is telling me totalFriends is undefined

const numImaginaryFriends = totalFriends => Math.round(totalFriends * .33);

const numImaginaryFriends = totalFriends => {
  Math.round(totalFriends * 0.33);
  return totalFriends;
};
console.log(numImaginaryFriends(5));

why float is not shown but console statement is shown instead?

Nothing has changed. We could assign the line above that to a temporary variable, say, temp = Math.round(...) and then return,

totalFriends * temp

Hello all, I came up with a code which passed from the challenge. However when I checked the solution, I see some differences. I would really appreciate if my version is bod/wrong/not recommended in any way? Thank you.

My code;

const numImaginaryFriends = (realFriends) => {
    return (Math.round(realFriends * 0.33));
}

Codecademy solution;

const numImaginaryFriends = totalFriends => Math.round(totalFriends * .33)

For the sake of the exercise, it’s recommended to always use the variable names given in the instructions. Just saying.

As for the code itself, there is nothing wrong or bad about the method you used. It’s the verbose form of an arrow function written in the exact form of an ES5 function.

a concise body is one in which all the syntax is relaxed when there is a single expression.

const foo = (arg) => {
    return arg
}

Since there is a single argument and a single expression we can,

  • drop the parens on the parameter
  • drop the curly braces from the body
  • drop the return keyword

The above becomes,

const foo = arg => arg;
2 Likes

It was very informative, thank you, highly appreciated.

1 Like