JavaScript Practice: Data Types, Conditional, Functions (Problem #3)

Create a function numberDigits() that takes the variable x as its only parameter. If the variable x is between 0 and 9, return the string 'One digit: N ' , where N is the value of x . If the variable x is between 10 and 99, return the string 'Two digits: N ' , where N is the value of x . Any other value of x , including negative numbers, return the string 'The number is: N ' , where N is the value of x .

The link to the problem (https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-practice-javascript-syntax-variables-data-types-conditionals-functions/articles/fecp-javascript-practice-data-types-conditional-functions)

My code would return unexpected results. I’m obviously doing something wrong here. Some help would be appreciated!

// Create function here
function numberDigits(x) {
if(x => 0 && x <= 9) {
return One digit: ${x};
}
else if(x => 10 && x <= 99) {
return Two digit: ${x};
}
else {
return The number is: ${x};
}
}

console.log(numberDigits(11));

You’re using => instead of <=, so it’s not working the way you want.

Example:

console.log(x => 0);

would output [Function] instead of true or false

Also, to pass the check, be sure to follow exactly what text they want in the output. You have “Two digit” instead of “Two digits”

1 Like

That solved it. Thanks!

Can someone help, please!! How can I solve this exercise?

function numberDigits(x) {
if (x >= 0 && x <= 9) {
return ‘One digit:’ + (x);
} else if (x >=10 && x <=99) {
return ‘Two digit:’ + (x);
} else ‘The number is:’ + (x);
}
console.log(numberDigits(333));
// Undefined

To preserve code formatting in forum posts, see: [How to] Format code in posts

Here are a few things to consider:

  • Your strings must match the strings specified in the instructions EXACTLY.
    Spelling, Spacing, Punctuation etc. much match exactly.
    You wrote    Two digit:    instead of    Two digits:   
    Also, it seems you don’t have a space between the colon and the digit. Your returned string seems to be in the format    "One digit:N"    instead of the specified format    "One digit: N"

  • You are not returning the string from the else block.

// You wrote:
} else 'The number is:' + (x);

// Consider changing to:
} else return 'The number is: ' + (x);

// OR
} else {
    return 'The number is: ' + (x);
}

// OR
} else {
    return `The number is: ${x}`;
}