DEV Community

Kauress
Kauress

Posted on • Updated on

Get comfortable with JS arrays: exercises for beginners

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 forEach() and filter() methods in functional JS.

The learner also had some confusion data types in arrays and about Parameters vs Argument so the exercises also reflect those.

1.

let myAlphabet = ['A', 'B', 'C', 'D','E','F', 'G'];
Enter fullscreen mode Exit fullscreen mode
  • What is the length of the array?
  • Write a function called myAlphabetLength which console.logs the length of the array
  • Within the function also use an if-conditional statement that checks if the number of items within the array are less than 4

2.

  • Declare a function checkFunc that takes a string and a boolean as parameters
  • Call the function using 2 arguments

3.

  • Declare and initialize an array called Planets with 5 string values
  • console.log each item in the array
  • Also console.log the index in each iteration

4.

  • Declare and initialize an array called
wowDatatypes
Enter fullscreen mode Exit fullscreen mode
  • The array must have 5 different data types (NOT objects)
  • Iterate over the array and console.log each item in the array + it’s index and data type in the array

5.

  • console.log each item in this array WITHOUT using a for loop
   let myArr = [ 1, 2, 'One', true];
Enter fullscreen mode Exit fullscreen mode

6.

let student1Courses = ['Math', 'English', 'Programming'];
let student2Courses = ['Geography', 'Spanish', 'Programming'];
Enter fullscreen mode Exit fullscreen mode
  • Loop over the 2 arrays and if there are any common courses, if so console.log them

7.

let food = ['Noodle', 'Pasta', 'Ice-cream'];
let food = ['Fries', 'Ice-cream', 'Pizza'];
Enter fullscreen mode Exit fullscreen mode
  • compare the 2 arrays and find common food if any

8.

let values1= ['Apple', 1, false];
let values2 = ['Fries', 2 ,true];
let values3 = ['Mars', 9, 'Apple'];
Enter fullscreen mode Exit fullscreen mode
  • compare the 3 arrays and find any common elements

9.

let furniture = ['Table', 'Chairs','Couch'];
Enter fullscreen mode Exit fullscreen mode
  • For each item in this array console.log the letters in each item

Discussion (10)

Collapse
tripol profile image
Ekanem • Edited on

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 on

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 Author

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 on

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 Author

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
mj_04 profile image
Manaswini Janaswamy

can you please share solutions

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