forked from DevMountain/javascript-3-afternoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestructuring.js
More file actions
126 lines (94 loc) · 3.25 KB
/
destructuring.js
File metadata and controls
126 lines (94 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Once you complete a problem, refresh ./destructuring.html in your browser and check to see if the problem's test(s) are passing.
Passed tests will be indicated by a green circle.
Failed tests will be indicated by a red X.
You can refresh the page at any time to re-run all the tests.
*/
////////// PROBLEM 1 //////////
// Do not edit the code below.
var carDetails = {
color: 'red',
make: 'toyota',
model: 'tacoma',
year: 1994
}
// Do not edit the code above.
/*
Use object destructuring to save the property values from the object carDetails into new variables.
*/
//Code Here
const {color, make, model, year} = carDetails;
////////// PROBLEM 2 //////////
/*
In the function below named greeting, it is receiving an object as a parameter.
Use object destructuring to save the object properties to new variables.
The property names are firstName, lastName, and title.
*/
function greeting( obj ) {
//Code Here
const {title, firstName, lastName} = obj;
// Do not edit the code below.
return 'Hello, ' + title + ' ' + firstName + ' ' + lastName + '!';
// Do not edit the code above.
}
////////// PROBLEM 3 //////////
/*
Write a function called totalPopulation that will take in an object.
That object will have 4 properties named utah, california, texas and arizona.
The property values will be numbers.
Use object destructuring to save the property values to new variables.
Sum up the values and return the total number.
*/
//Code Here
function totalPopulation(obj){
const {utah, california, texas, arizona} = obj;
return utah + california + texas + arizona;
}
////////// PROBLEM 4 //////////
/*
Write a function called ingredients that will take in an object.
This object will have 3 properties named carb, fat, and protein.
The property values will be strings.
Use object destructuring to save the property values to new variables.
Push these new variables to an array and return the array.
*/
//Code Here
function ingredients(obj){
const {carb, fat, protein} = obj;
const arr = [];
arr.push(carb, fat, protein);
return arr;
}
ingredients({carb: 1, fat: 2, protein: 3});
////////// PROBLEM 5 //////////
/*
Now we will use object destructuring as the function's parameter instead of destructuring the object inside of the function declaration.
Example:
function example( {one, two, three} ) {
return one + two + three
}
Write a function called largeNumbers that will take a destructured object as it's parameter.
The object properties will be named first, second, and third and their values will be numbers.
Find the smallest number of the three and return that number.
*/
//Code Here
function largeNumbers({first, second, third}){
return [first, second, third].sort((a,b) => a - b)[0];
}
////////// PROBLEM 6 //////////
/*
Write a function called numberGroups that will take a destructured object as it's parameter.
The object properties will be named a, b, and c and their values will be arrays of numbers.
Find the longest array and return that array.
*/
//Code Here
function numberGroups({a, b, c}){
const arr = [a, b, c];
let longest = [];
for (var i = 0; i < arr.length; i++){
if (arr[i].length > longest.length){
longest = arr[i];
}
}
return longest;
}