FAQ: Code Challenges: JavaScript Fundamentals - numImaginaryFriends()

const numImaginaryFriends = (totalFriends) => {
return Math.round(totalFriends * 1/3);
};
console.log(numImaginaryFriends(18));

hi! im doing this exercise now and i dont see where i went wrong. please take a look? thanks in advance

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

You’re very close, except the return keyword should precede the expression, not follow it.

return expression

Aside

For the sake of readability it is common to write a leading zero before the decimal point.

0.33

im sorry why is it coming before? it usually comes after.

Math.round(totalFriends * .33) is the expression we wish to return, so,

return Math.round(totalFriends * 0.33)

thank you very much. i seem to get stuck translating normal linguistics to computer code or logic. is there something you can recommend to help me make that translation?

Start or end each day’s session with some reading, whether articles relating to the day’s lessons or the documentation itself. Eventually the terminology and diction will begin to gel. Review each unit that you complete, and add items to your reading list. Paste the URLs to readings into your completed lessons so you can go back and review those, as well.

1 Like

So i did it like this:
const realFriends = 100;

const percentToGet = 33;

const numImaginaryFriends = Math.round((percentToGet / 100) * realFriends);

console.log(numImaginaryFriends);
and it worked fine but would not be accepted, i would like to know why this is the wrong way of doing it please

I believe the exercise wants you to create a function that will accomplish this task and return the correct value. Try implementing your code as a function and see if that works.

Welcome to the forums!

Thanks, appreciate it

1 Like

Continuing the discussion from FAQ: Code Challenges: JavaScript Fundamentals - numImaginaryFriends():

Hello, I’m curious as to why I’m returning “undefined” in my console with the following code.

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

console.log(numImaginaryFriends(18));

Thanks guys.

A function with no hard coded return value will return undefined by default. Have your function return the value. As it stands it goes into thin air.

Thank you for your help mtf.

1 Like

My code seems to work and I’m using the Math.round function but the checker keeps rejecting it, what am I doing wrong?

// Write your function here:

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

console.log(numImaginaryFriends(18)) // Should print 6

// We encourage you to add more function calls of your own to test your code!

What error message are you given?

Someone should help me with this. Why would the default block run?? when I run
console.log(numImaginaryFriends(18));
Here is the code

function numImaginaryFriends(x){

switch(x){
case (x >= 1):
return x *= 0.33;
break;

default:
return "Oops, You don't have any friends"

}
}

if you want to use a condition x >= 1, it’s better to use an if statement.

if(x >= 1) {
  return x *= 0.33
} else {
  return "Oops, You don't have any friends"
}