JavaScript Simplified / Chapter 40 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to CodePen.


Are you ready to rate JavaScript Simplified? on Amazon?


Rate it on Amazon.

0,1,2,3,4,5,6,7,8,9

0

0

0

0

What is the keyword that converts "1.5" to 1? parseInt parseInt
Number is a keyword that converts "1.5" to 1.5. What is another keyword that does the same thing? parseFloat parseFloat
Convert the string in the variable price to a decimal number if the string is a decimal or an integer if the string is an integer. Assign the result to the same variable. price = parseFloat(price); price=parseFloat\(price\);
A string has been assigned to the variable x and another string assigned to the variable y. The strings may or may not be integers. In a single statement add x converted to an integer plus y converted to an integer. (Convert and add all in the same statement). Assign the result to tot, which hasn't been declared beforehand. let tot = parseInt(x) + parseInt(y); lettot=parseInt\(x\)\+parseInt\(y\);
Convert a string represented by a variable to an integer if the string is an integer, or to a decimal number is the string is a decimal number. Assign the result to a second variable that hasn't been declared beforehand. Make up the variable names. let myNum = Number(myString); let[a-z_$][a-zA-Z0-9_$]*=Number\([a-z_$][a-zA-Z0-9_$]*\);
Convert a string represented by a variable to a decimal number regardless of whether the string is a decimal or an integer. Assign the result to a second variable that hasn't been declared beforehand. Make up the variable names. let myNum = parseFloat(myString); let[a-z_$][a-zA-Z0-9_$]*=parseFloat\([a-z_$][a-zA-Z0-9_$]*\);
The string variable, already declared, is num. Convert it to an integer if the string contains an integer, or to a decimal if the string contains a decimal. Assign the result to the same variable. num = Number(num); num=Number\(num\);
In a single statement, code an alert that displays the sum of 2 different strings represented by variables, converted to decimal numbers. The strings may or may not be integers. Make up the variable names. alert(parseFloat(str1) + parseFloat(str2)); alert\(parseFloat\([a-z_$][a-zA-Z0-9_$]*\)\+parseFloat\([a-z_$][a-zA-Z0-9_$]*\)\);
  1. Assign the string "5.03" to a variable that hasn't been declared beforehand.
  2. Code an alert that displays the sum of the number, converted to an integer, added to itself.
  3. Use the keyword of your choice for the conversion.
  4. Click the Result button (or, after revising, don't click, just wait).
  5. Wait a moment.
  6. If you've coded correctly, an alert will display the total.
  7. Dismiss the alert by clicking OK.
  8. For help with this code, see Chapter 40 in the book.
  1. Assign the string "1.02" to a variable.
  2. Code an alert that displays the sum of the number, converted to a number that preserves the decimal, added to itself.
  3. Use the keyword of your choice for the conversion.
  4. Click the Result button (or, after revising, don't click, just wait).
  5. Wait a moment.
  6. If you've coded correctly, an alert will display the total.
  7. Dismiss the alert by clicking OK.
  8. For help with this code, see Chapter 40 in the book.