JavaScript Simplified / Chapter 36 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 for finding a character at a particular location in a string? charAt charAt
In this string, what character is at index 2?— Don't include quotation marks in your answer. "abcde" c •?c•?
Find the 10th character in the string represented by txt and assign it to the variable char, which hasn't been declared beforehand. let char = txt.charAt(9); letchar=txt\.charAt\(9\);
Find the last character in the variable x's string and assign it to y, which hasn't been declared beforehand. let y = x.charAt(x.length - 1); lety=x\.charAt\(x\.length-1\);
Find the first character in a string represented by str and assign it to x, which hasn't been declared beforehand. let x = str.charAt(0); letx=str\.charAt\(0\);
Find the last character in a variable's string and assign it to a variable that hasn't been declared beforehand. Make up the variable names. let x = str.charAt(str.length - 1); let[a-z_$][a-zA-Z0-9_$]*=([a-z_$][a-zA-Z0-9_$]*)\.charAt\(\1\.length-1\);
Code the first line of an if statement that tests whether the 3rd character of a variable's string is a particular character. Make up the variable name and the string. if (str.charAt(2) === "a") { if\([a-z_$][a-zA-Z0-9_$]*\.charAt\(2\)===•.*•\){
  1. Code a for loop that cycles through all the characters of a variable's string and assigns each character to an element of an array that has been declared beforehand.
  2. Use i and <.
  3. Use push.
  4. Make up everything.
for (let i = 0; i < txt.length; i++) {
  txtArray.push(txt.charAt(i));
}
for\(leti=0;i<([a-z_$][a-zA-Z0-9_$]*)\.length;i\+\+\){[\r\n][a-z_$][a-zA-Z0-9_$]*\.push\(\1\.charAt\(i\)\);[\r\n]}
  1. Assign a string to a variable.
  2. Using the charAt keyword, code an alert that displays the first character of the string.
  3. Click the Result button (or, after revising, don't click, just wait).
  4. Wait a moment.
  5. If you've coded correctly, an alert will display the first character.
  6. Dismiss the alert by clicking OK.
  7. For help with this code, see Chapter 36 in the book.
  1. Assign a string to a variable.
  2. Using the charAt and length keywords, code an alert that displays the last character of the string.
  3. Click the Result button (or, after revising, don't click, just wait).
  4. Wait a moment.
  5. If you've coded correctly, an alert will display the last character.
  6. Dismiss the alert by clicking OK.
  7. For help with this code, see Chapter 36 in the book.