Front-end-engineer JS practice conditionals, data types, functions

Hi,

I am working on this section in the front-end-engineer career path and ran into something I think is odd. Now my code works, however it is not the solution given but similar, I tested all my if functions and they work as I would expect them too. Now I can simply change it to the given solution but I want to understand why it believes my code fails when as far as I can tell it looks the same as the provided solution minus so variable names. I am curious what you all think, would you accept my code as a solution or am I not using good practices with my code? My code is below along. Oddly enough the error given shows my code is correct as it says it expects A and gets A.

I guess my question really is more for me to learn why it thinks it is wrong more so than finding out if it is wrong. Hope that makes sense.
EDIT: link to the question
it is the last of the three questions.

EDIT2: below I used num as the variable, I also tried it as it asks with ‘x’ as the variable and I get the same thing.


const numberDigits = num => {

if (num >= 0 && num <= 9){

return `'One digit: ${num}'`

}

if (num >=10 && num <= 99) {

return `'Two digits: ${num}'`

}

if (num >= 100 || num < 0) {

return `'The number is: ${num}'`
}
}
console.log(numberDigits(0))

the error given in the console is this.

Tested numberDigits(0), expected: 'One digit: 0' but received: 'One digit: 0'.

I believe it is due to the way you have constructed your strings.

The answer is expecting

One Digit: 0

Your code is creating the following string:

'One Digit: 0'

Thanks for your reply, I tried that also with and without quotes in the answer, actually the solution points to using quotes in the answer so I assume this is what it wants. From what I can tell the only thing I did not try is using another variable in my code instead of using just X as it asks, I kept it the way I did because the actual question doesn’t ask for a second variable but does ask for the response to be in single quotes ‘’.

I can confirm, it is the use of the single quotes inside of the string that is causing the issue. When I remove them from every statement the code passes.

very interesting, thanks for having a look. I was certain that was not it considering the wording in the question with examples. Thanks!

1 Like