DEV Community

Get comfortable with JS arrays: exercises for beginners

Kauress on January 03, 2020

Just a few exercises that I came up with while tutoring. The goal is to wrap your head around iterating over arrays as a pre-cursor to learning the...
Collapse
 
tripol profile image
Ekanem • Edited

Some quick solutions

5)

let myArr = [ 1, 2, 'One', true];

let newArray = myArr.map(item => `This item is ${item}`)
console.log(newArray)
Enter fullscreen mode Exit fullscreen mode

6)

let student1Courses = ['Math', 'English', 'Programming'];
let student2Courses = ['Geography', 'Spanish', 'Programming'];

let commonCourse = [...student1Courses, ...student2Courses].reduce((total, item, index, array) => {
  (array.indexOf(item, index + 1) !== -1 && total.indexOf(item) === -1) ? total.push(item) : null
  return total
}, [])

console.log(commonCourse)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
camicode profile image
CamCode • Edited

Hi @kauresss ,
thanks for these exercises!!!
I completed all and they help me to understand better Array

P.s I don't if it's error or not , but in eighth exercise there are same name variable ( values2)

Collapse
 
kauresss profile image
Kauress

You're welcome! And thanks for pointing that out, I've corrected it :)

Collapse
 
des3000 profile image
des3000

Hello, can you give link to solutions of this exercises? I don't know if I am doing them correctly

Collapse
 
mahmoudalawad profile image
mahmoud

Some solutions (6)

let student1Courses = ['Math', 'English', 'Programming', 'd'];
let student2Courses = ['Geography', 'd', 'Spanish', 'Programming'];

const filterArray = student1Courses.filter(value => student2Courses.includes(value));

function filterArray(){
const diff = [];
for (let i = 0; i < student1Courses.length; i++) {
const val1 = student1Courses[i];

  for (let j = 0; j < student2Courses.length; j++) {
      const val2 = student2Courses[j];


  if (val2.includes(val1) ) {

    diff.push(val2);
    console.log(diff);

}
  }

}
Enter fullscreen mode Exit fullscreen mode

}

filterArray();

Collapse
 
lucarioowns profile image
Roy Frias • Edited

Hello Kauress I was wondering if you can give us a link to the solutions to the problems please , I want to make sure that I am doing them correctly, if you can be so kind.

sincerely Roy Frias

Collapse
 
kauresss profile image
Kauress

Hello Roy, sure I'll put up the solutions soon. I'm finishing a 400+ coding exercises workbook after which I'll get some free time

Collapse
 
lucarioowns profile image
Roy Frias

Ah Ok cool, Thank you so much !!

Collapse
 
brogeby profile image
brogeby

Solution to 9)

const furniture = ['Table', 'Chairs', 'Couch']
furniture.forEach(e => console.log(e.split('')))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
scarfada profile image
scarfada

**_

Question 4

_**

var wowDatatypes = ['Scarlaty', 26, true, null, ['Cool']]

wowDatatypes.forEach(differentDataType)

function differentDataType(item, index){
console.log('['+index+']'+ " " +item)
}

Collapse
 
mj_04 profile image
Manaswini Janaswamy

can you please share solutions

Collapse
 
scarfada profile image
scarfada

**

Question 3

**

var Planets = ['Vênus', 'Marte', 'Mercúrio','Urano','Jupter','Netuno']
Planets.reverse()

Planets.forEach(Interation)

function Interation(item, index){

console.log('['+index+']', item)
}

Collapse
 
aizenmarina profile image
aizenmarina

Hi Kauress, I really liked these exercises because they are just what I needed to practice (and force myself to find out how to solve them! ) Could you please share more? Many thanks! ;)

Collapse
 
joshlanuevo profile image
Josh Ivan Lanuevo

Solution # 3

const Planets = ["mercury", "venus", "earth", "mars", "jupiter"];

Planets.forEach((element,index) => console.log(element,index));

Collapse
 
scarfada profile image
scarfada

Question 5

let myArr = [ 1, 2, 'One', true];

myArr.filter(passandoPelaArray)

function passandoPelaArray(filtrando){

console.log(filtrando)
}