Non-Standard For-Loops

So far, you've seen for-loops the same way: iterating one by one; but for-loops are much more flexible than that and can be used in many different situations.

In this reading, you will:

Revisiting the For-Loop

So far, you've probably been using it the same way to loop through a string like so:

const str = "hello";
for (let i = 0; i < str.length; i++) {
    console.log(str[i]);
}
/*
Prints:
h
e
l 
l
o
*/

This is the most common usage of a for-loop and can be considered somewhat of a standard for-loop. However, this isn't the only way to use a for-loop.

There's nothing stopping you from starting i from something other than 0, or stopping at an index other than arr.length, or even incrementing i by more than 1 at a time.

Case 1: Printing Odd Indexes

Say you have the string below and you want to get all the odd index values.

let str = 'academy'; //=> convert to 'cdm'

One way to do this could look like the following:

let newStr = '';
for (let i = 0; i < str.length; i++) {
    // Check if i is odd
    if (i % 2 === 1) {
        // Add character to new string only if conditition is satisfied
        newStr += str[i];
    }
}
console.log(newStr); //=> 'cdm'

This will work as expected and you will see 'cdm' printed in the console, but this can be cleaned up by changing the way the for-loop is declared.

Instead, you could start from the first odd index, 1, and increase i by 2 every time instead of 1 since you know every other number is odd. Taking this into account, you can construct your for-loop as follows:

let newStr = '';
for (let i = 1; i < str.length; i += 2) {
    newStr += str[i];
}
console.log(newStr); //=> 'cdm'

This takes advantage of the for-loop's ability to execute the logic instead of defining the logic within the loop.

This also saves time on execution because you will no longer have to loop through every index just to get the odds; you loop directly through all the odds and skip the evens, cutting your runtime in half. You could imagine that with a very large string, that execution time can become significantly decreased.

Case 2: Iterating Backwards

Say you want to implement a function that reverses a string. You could implement the function so that it iterates through the string and adds each character to the beginning of the new string:

function reverseStr(str) {
    let newStr = '';
    // Iterate from the front of the string to the back of the string
    for (let i = 0; i < str.length; i++) {
        // Add the character at i to the FRONT of the string
        newStr = str[i] + newStr;
    }
    return newStr;
}

console.log(reverseStr('academy')); //=> 'ymedaca'

Or you can use define your for-loop differently so that i starts at the end of the string and decrements after every loop until it reaches an index i of 0:

function reverseStr(str) {
    let newStr = '';
    // Iterate from the back of the string to the front of the string
    for (let i = str.length - 1; i >= 0; i--) {
        // Add the character at i to the BACK of the string
        newStr += str[i];
    }
    return newStr;
}

console.log(reverseStr('academy')); //=> 'ymedaca'

Although these two different definitions of the for-loop do the same thing, one way may be easier to read than the other. You can choose to implement whichever way you'd like. However, you should try practicing the second way so that if you ever encounter it in someone else's code, you will be able to read it and understand it fluently.

What you learned

In this reading, you explored more use cases of the for-loop by taking advantage of the different components of a for-loop.