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);
Roffy bc
  • 91
  • 1
  • 8
  • from index 1 to 5 should be ```cars.slice(1,6)``` – tcf01 Jun 29 '20 at 01:22
  • the tasks asks me to do it in loop – Roffy bc Jun 29 '20 at 01: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 – Phil Jun 29 '20 at 01:34
  • I tried to do it but my brain is over exhausted,sorry for bothering you more but can you give me simple example? – Roffy bc Jun 29 '20 at 01:35
  • What happens if the array is shorter than 5? What happens if it's shorter than 1? What should the result be for these edge-cases? – Phil Jun 29 '20 at 01:35
  • The task is just to loop over an existing array and console.log item from index 1 to 5 , that cases does not exist in task – Roffy bc Jun 29 '20 at 01:36
  • Also array is given in task already,It s not created manually – Roffy bc Jun 29 '20 at 01:37
  • 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](https://stackoverflow.com/posts/62629856/edit) to describe **exactly** what you're trying to do and what should happen in unfavourable conditions – Phil Jun 29 '20 at 01:40
  • Sorry, i fixed it – Roffy bc Jun 29 '20 at 01:41
  • 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. – iAmOren Jun 29 '20 at 02:06

4 Answers4

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");
};
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
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);

Aditya Joshi
  • 1,033
  • 7
  • 20
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);
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
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);
Praneet Dixit
  • 1,393
  • 9
  • 25