Ask coding questions

← Back to all posts
Student learning strings
mariav013 (1)

This is how far I got on the following instructions. Can someone help point me in the right direction. I can't figure out how to not include the decimal.

Exercises: Strings (Part 1)
The length method returns how many characters are in a string. However, the method will NOT give us the length of a number. If num = 1001, num.length returns undefined rather than 4.

  1. Use type conversion to print the length (number of digits) of an integer.

  2. Change num to 123.45.
    Modify your code to print out the length of a decimal value EXCLUDING the period.

Example: The number 123.45 has a length of 6 but should print 5 for the number of digits.

  1. What if num could be EITHER an integer or a decimal?
    Add an if/else statement so your code can handle both cases.
    Hint: Consider the indexOf() or includes() string methods.

The above code will print out 6. It should be 5 because the decimal point is to be ignored. First you need to find if a given number has a decimal point. You can use the includes function. if the decimal point is present then take 1 off of the length of the number and display it else display the length of the number string