JavaScript fundamentals
Theory: Traversing a string in a loop
You can use loops to process strings as well as numbers. Mainly as a method for retrieving a specific character by its index. Below is an example of code that prints each letter of a word on a single line:
The right test condition for the while loop is essential in this code. You can do it two ways: i < name.length or i <= name.length - 1. Both lead to the same result.
Building strings with loops
Loops also allow you to generate strings. It's a common task for web development. It's merely basic aggregation using interpolation or concatenation.
One especially popular task among interviewers is to do a string reversal. There are lots of ways to solve it, but the most basic is by iterating each character. Have a look at how this function works:
The general idea of the reversal is that you take characters one by one from the beginning of the string and then connect them in reverse order. Sounds like a piece of cake. Now let's examine it:
Perhaps the only tricky part here is getting a feel of how the string itself is built. Since each next character is attached to the resulting string on the left, the string ends up reversed.
Recommended programs
Completed
0 / 39

