0

How can i console.log items from index 1 to 5 in current array? Using Loop

let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"]

const mappedCars=cars.map((item, i) => {
    console.log("The current index is: " + i);
    console.log("The current element is: " + item);
    console.log("\n");
    return item; //equivalent to list[index]
});

console.log(mappedCars);

10
  • from index 1 to 5 should be cars.slice(1,6) Commented Jun 29, 2020 at 1:22
  • the tasks asks me to do it in loop Commented Jun 29, 2020 at 1:26
  • If you must use a loop, you'll need to create an empty array, loop over the indexes you want and push the entries in to the new array Commented Jun 29, 2020 at 1:34
  • 1
    Your question says "How can i return items..." which is quite different to just console.log(). You should always be thinking of boundary conditions for your code. Please edit your question to describe exactly what you're trying to do and what should happen in unfavourable conditions Commented Jun 29, 2020 at 1:40
  • 2
    for(var i=1; i<=Math.min(5, cars.length-1); i++) console.log("Index="+i+" Value="+cars[i]); If there are less than two cars, no output. if there are less than 6 cars, output will be less than 5 cars. Commented Jun 29, 2020 at 2:06

4 Answers 4

3

With a for loop:

let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"]

for(i = 1; i<cars.length; i++){
    console.log("The current index is: " + i);
    console.log("The current element is: " + cars[i]);
    console.log("\n");
};

Sign up to request clarification or add additional context in comments.

Comments

3

With for loop you can define the initial condition as well as loop terminating condition.

for ([initialExpression]; [condition]; [incrementExpression])
 statement
let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"]

const updated = [];
for (let i=1; i<=5; i++) {
        updated.push(cars[i]);
}

console.log(updated);

Comments

2

To do this in a loop for indexes 1 to 5 (inclusive):

let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"]

const filteredCars = [];
for (let i=1; i<=5; i++) {
        filteredCars.push(cars[i]);
}

console.log(filteredCars);

Without loop you may also use filter() or slice(). See example with filter():

let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"]

const filteredCars=cars.filter((item, i) => i>=1 && i<=5);

console.log(filteredCars);

3 Comments

Thank you very much
Why are you performing unnecessary iterations. for (let i = 1; i <=5; i++) seems more appropriate
Edited - Thanks
1

You can do it with forEach :

let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"];

function repea (element, index) {
  console.log(`The current index is: ${index}`);
  console.log(`The current element is: ${element}`);
}


cars.forEach(repea);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.