forked from DevMountain/javascript-3-afternoon-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destructuring.js
134 lines (105 loc) · 3.27 KB
/
destructuring.js
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
127
128
129
130
131
132
133
134
/*
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
var {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 ) {
let {firstName, lastName, title} = 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) {
let {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) {
let {carb, fat, protein} = obj
let newArr =[]
newArr.push(carb, fat, protein)
return newArr
}
////////// 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}) {
let numArr = []
numArr.push(first,second,third)
return Math.min(...numArr)
}
////////// 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}) {
if(a.length > b.length) {
if (a.length > c.length){
return a
}
else {
return c
}
}
else {
if(b.length > c.length) {
return b
}
else{
return c
}
}
}
// return longest