Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I print the 1st and 2nd elements on a line, then 2nd and 3rd on the next line, then 3rd and 4th next line, and so on

I’m trying to print array elements 1st and 2nd in one line, then next line 2nd and 3rd elements, but I’m still stuck.

Input:

const arr = [10, 20, 30, 40, 50];

Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

10 - 20
20 - 30
30 - 40
40 - 50

>Solution :

for (let i = 0; i < arr.length - 1; i ++) {
  console.log(arr[i] + ' - ' + arr[i+1]);
}

but I’d recommend using the syntax

console.log(`${arr[i]} - ${arr[i+1]}`)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading