Rest & Spread operators exercises
All executions of the exercises will display on the console!
Exercise 1
Function declaration:
const numbers1 = [1, 2, 3, 5, 8]
const numbers2 = [2, 4, 6, 8, 10]
const allNumbers = [...numbers1, ...numbers2]
Code for its execution:
console.log(allNumbers)
Exercise 2
Function declaration:
const addition = (...numbers) => {
let total = 0
for (let number of numbers) {
total += number
}
return total
}
Code for its execution:
const numbers = [1, 2, 3]
console.log(addition(...numbers))
Exercise 3
Object declaration and copy:
const fiatPanda2000 = {
brand: 'FIAT',
model: 'PANDA',
engine: '2.0',
}
const fiatPanda2001 = { ...fiatPanda2000 }
fiatPanda2001.engine = '2.2'
Code to show them:
console.log(fiatPanda2000)
console.log(fiatPanda2001)
Exercise 4
Array declarations:
const fruits = ['apple', 'bannana', 'pineapple', 'orange', 'strawberry']
const [fruit1, fruit2, ...allOtherFruits] = fruits
Code to show them:
console.log(`The first fruit is ${fruit1}`)
console.log(`The second fruit is ${fruit2}`)
console.log(`All the other fruits are ${allOtherFruits}`)
Exercise 5
Function declaration:
const multiply3Numbers = (a, b, c) => {
return a * b * c
}
Code for its execution:
const numbersToMultiply = [1, 3, 4]
console.log(multiply3Numbers(...numbersToMultiply))
Exercise 6
Objects declaration:
const stone = {
color: 'gray',
durability: '25',
}
const axe = {
toolName: 'axe',
use: 'cuting trees',
}
const stoneAxe = { ...stone, ...axe}
Code to show them:
console.log(stoneAxe)