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'];
- 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 than4
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
- 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];
6.
let student1Courses = ['Math', 'English', 'Programming'];
let student2Courses = ['Geography', 'Spanish', 'Programming'];
- 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'];
- 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'];
- compare the 3 arrays and find any common elements
9.
let furniture = ['Table', 'Chairs','Couch'];
- For each item in this array
console.log
the letters in each item
Top comments (15)
Some quick solutions
5)
6)
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)
You're welcome! And thanks for pointing that out, I've corrected it :)
Hello, can you give link to solutions of this exercises? I don't know if I am doing them correctly
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];
}
filterArray();
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
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
Ah Ok cool, Thank you so much !!
can you please share solutions
**_
Question 4
_**
var wowDatatypes = ['Scarlaty', 26, true, null, ['Cool']]
wowDatatypes.forEach(differentDataType)
function differentDataType(item, index){
console.log('['+index+']'+ " " +item)
}
**
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)
}
Solution to 9)
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! ;)