DEV Community

mfb
mfb

Posted on

Week 2 Colt Steele JavaScript. Of Loop is an easier way of writing a for loop

Here is a way of how to get the same outcome using these two methods, for loop (which is the longer way) and of loop which is the shorter way.

This here under should be the outcome

Kristen
Erik
Namita
Geoffrey
Juanita
Antonio
Kevin
Yuma
Sakura
Jack
Erika

const seatingChart = [
['Kristen', 'Erik', 'Namita'],
['Geoffrey', 'Juanita', 'Antonio', 'Kevin'],
['Yuma', 'Sakura', 'Jack', 'Erika']
]

For Loop (longer way of doing this)

for (let i = 0; i < seatingChart.length; i++) {
const row = seatingChart[i];
for (let j = 0; j < row.length; j++){
console.log(row[j])
}
}

Of loop (shorter way of doing this)

for (let row of seatingChart){
for (let student of row) {
console.log(student)
}
}

Row represents the rows in seatingChart. Student represents the students inside each row.

Top comments (0)