Phone Home challenge: JS Methods do not work

I’ve worked on the answer, and when I test it in VS Code, it works perfectly. It returns exactly the same as the test cases, and even when I check it with typeof, it returns a number, but, when testing on FreeCodeCamp looks like using the methods toFixed() and parseFloat() breaks the program. and every test case returns bad. so I did some more research.

Even if I hardcode the answer to the first test case (2.5) as the return value, it doesn’t work unless I remove both methods. So does that mean it breaks the console? Or is it stated somewhere that those methods are not allowed? Maybe that’s the case, and if so, please tell me where so I can keep that in mind for future challenges.

However, doing the math conversions while avoiding those methods works fine.

function sendMessage(*route*) {

  let time = 0;

  for (let n = 0; n < *route*.length; n++) {

    const stretch = *route*\[n\];

    time += stretch / 300000;

    if(n === *route*.length - 1) time += 0.5

  }

  calc = time.toFixed(4)

  return parseFloat(calc)

}

there are various characters escaped so I am not sure what is exactly your code

but you should try to add a function call in the editor, you should see an error

freeCodeCamp is sensitive when it comes to variables, your calc = time.toFixed(4) should be given a declaration.

Also you mentioned about using .toFixed and parseFloat, seems working to my end through modifying my answer to the challenge.

1 Like
console.log(sendMessage([1000000, 500000000, 1000000])); // 1674.3333.
console.log(sendMessage([10000, 21339, 50000, 31243, 10000])); // 2.4086.
console.log(sendMessage([802101, 725994, 112808, 3625770, 481239])); // 21.1597.

Your code is not passing these tests on my end.

Thank you very much! I don’t know how I missed that detail, lol, what a shame.

You’re right! It turns out that while I was trying to find the reason, I intentionally modified the original line that adds the delay between satellites n === route.length - 1 ? null : (time += 0.5); with an if statement. I was confused and thought that assigning it to null could cause the problem.

Actually, the null would have worked, but your false expression would throw an error because you can’t assign values inside a ternary.

EDIT: On a whim, I just replaced the if statement in the code you originally posted with the ternary you just posted…you know, the one I thought would throw an error…and I’m surprised…it works! I’ve never seen a ternary with an assignment expression like that before. I even changed it to n === route.length - 1 ? null : time = time + 0.5; Still works! Thank you for showing me something I didn’t know about JS!!