Assignment 1
1. Solve the following questions
Exercise 1
Rewrite the code below to use array destructuring instead of assigning each
value to a variable.
{
console.log("EXERCISE 1");
let item = ["Egg", 0.25, 12];
let name = item[0];
let price = item[1];
let quantity = item[2];
console.log(`Item: ${name}, Quantity: ${quantity}, Price: ${price}`);
console.log();
}
/**
Exercise 2
Rewrite the code below to assign each number to the right variable.
*/
{
console.log("EXERCISE 2");
let numbers = [3, 5, 4, 2, 6, 1];
let [one, two, three, four, five, six] = numbers;
console.log(`One: ${one}, Two: ${two}, Three: ${three}, Four: ${four}, Five:
${five}, Six: ${six}`);
console.log();
}
/**
Exercise 3
We have an object called 'user'.
Write the destructuring assignment that reads:
- 'name' property into the variable 'name'.
- 'years' property into the variable 'age'.
- 'isAdmin' property into the variable 'isAdmin' (false, if no such property)
*/
{
console.log("EXERCISE 3");
let user = { name: "John", years: 30 };
// your code to the left side:
let {} = user;
console.log(name); // John
console.log(age); // 30
console.log(isAdmin); // false
console.log();
}
/**
Exercise 4
Rewrite the code below to use array destructuring instead of assigning each
value to a variable.
*/
{
console.log("EXERCISE 4");
let person = [12, "Chris", "Owen"];
let firstName = person[1];
let lastName = person[2];
let age = person[0];
console.log(`Person - Age: ${age}, Name: ${firstName} ${lastName}`);
console.log();
}
/**
Exercise 5
Rewrite the code below to use array destructuring instead of assigning each
value to a variable.
Make sure not to have unused variables.
Hint:
https://untangled.io/in-depth-es6-destructuring-with-assembled-avengers
*/
{
console.log("EXERCISE 5");
let person = ["Chris", 12, "Owen"];
let firstName = person[0];
let lastName = person[2];
console.log(`Name: ${firstName} ${lastName}`);
console.log();
}
/**
Exercise 6
Using Array Destructuring get the last name from the array.
Hint:
https://untangled.io/in-depth-es6-destructuring-with-assembled-avengers
*/
{
console.log("EXERCISE 6");
const students = ['Christina', 'Jon', 'Alexandare'];
// Write your code here
const [] = students;
console.log(lastName);
console.log();
}
/**
Exercise 7
Using Array Destructuring get all of the names from this Nested Array
Hint:
https://untangled.io/in-depth-es6-destructuring-with-assembled-avengers
*/
{
console.log("EXERCISE 7");
const moreStudents = [
'Chris',
['Ahmad', 'Antigoni'],
['Toby', 'Sam']
];
Exercise 8
Destructure the following into variables id and name as FirstName
const props = [
{ id: 1, name: "Raj" },
{ id: 2, name: "Richa" },
{ id: 3, name: "Radha" },
];
Exercise 9: Solve the following
1) Write a function that can take in any number of arguments, and returns the sum
of all of the arguments.
2) Write a function called addOnlyNums that can take in any number of arguments
(including numbers or strings), and returns the sum of only the numbers.
3) Write a function called combineTwoArrays that takes in two arrays as arguments,
and returns a single array that combines both (using the spread operator).
4) Write a function called sumEveryOther that takes in any amount of arguments,
and returns the sum of every other argument.
5) Write a function called onlyUniques that can take in any number of arguments,
and returns an array of only the unique arguments.
6) Write a function called combineAllArrays that takes in any number of arrays as
arguments and combines all of them into one array.
7) Write a function called squareAndSum that takes in any number of arguments,
squares them, then sums all of the squares.
Exercise 10
Read the following data carefully and solve the problems stated below
const constants = [2.72, 3.14, 9.81, 37, 100]
const countries = ['Finland', 'Estonia', 'Sweden', 'Denmark', 'Norway']
const rectangle = {
width: 20,
height: 10,
area: 200,
perimeter: 60
}
const users = [
{
name:'Brook',
scores:75,
skills:['HTM', 'CSS', 'JS'],
age:16
},
{
name:'Alex',
scores:80,
skills:['HTM', 'CSS', 'JS'],
age:18
},
{
name:'David',
scores:75,
skills:['HTM', 'CSS'],
age:22
},
{
name:'John',
scores:85,
skills:['HTML'],
age:25
},
{
name:'Sara',
scores:95,
skills:['HTM', 'CSS', 'JS'],
age: 26
},
{
name:'Martha',
scores:80,
skills:['HTM', 'CSS', 'JS'],
age:18
},
{
name:'Thomas',
scores:90,
skills:['HTM', 'CSS', 'JS'],
age:20
}
]
1) Destructure and assign the elements of constants array to e, pi, gravity,
humanBodyTemp, waterBoilingTemp.
2) Destructure and assign the elements of countries array to fin, est, sw, den, nor
3) Destructure the rectangle object by its properties or keys.
4) Iterate through the users array and get all the keys of the object using
destructuring
5) Find the persons who have less than two skills
6) Destructure the countries object print name, capital, population and languages of
all countries
7) A junior developer structure student name, skills and score in array of arrays
which may not easy to read. Destructure the following array name to name, skills
array to skills, scores array to scores, JavaScript score to jsScore and React score
to reactScore variable in one line.
8) Write a function called convertArrayToObject which can convert the array to a
structure object.