• Post author:
  • Post category:Blog
  • Reading time:2 mins read
  • Post last modified:June 12, 2024

We store an array of animal names in the animals variable (e.g. let animals = ["dog", "cat", "hamster", "rabbit"];).

Which of the following statements will display exactly two names from the array?

  • for (let i =3 ; i < animals.length; i++)
         console.log(animals[i]);
  • for (let n in animals)
         console.log(n);
  • for (let n of animals)
         console.log(n);
  • for (let i = 0; i < animals.length; i+=2)
         console.log(animals[i]);
Explanation & Hint:

In this code, the for loop starts at index 3 (the fourth element) of the animals array (i = 3), and continues iterating as long as i is less than the length of the animals array (i < animals.length). In each iteration, it logs the value at index i of the animals array (console.log(animals[i])). Since the for loop starts at index 3 and continues until the end of the array, it will display exactly two names from the array, assuming there are at least two elements in the array after index 3.

For more Questions and Answers:

JavaScipt Essentials 1 (JSE) | JSE1 – Module 4 Test Exam Answers Full 100%

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments