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
123 lines (90 loc) · 3.17 KB
/
destructuring.js
File metadata and controls
123 lines (90 loc) · 3.17 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
/*
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.
*/
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 ) {
var { 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.
*/
function totalPopulation (object) {
var {utah, california, texas, arizona} = object;
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.
*/
function ingredients (object) {
var {carb, fat, protein} = object;
var array = [];
array.push(carb, fat, protein);
return array
// return object.push[];
}
////////// 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.
*/
function largeNumbers ( { first, second, third } ) {
var number = Math.min(first, second, third);
return number
}
////////// 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.
*/
function numberGroups ( { a, b, c} ) {
if (a.length > b.length && a.length > c.length) {
return a
}
if (b.length > a.length && b.length > c.length) {
return b
}
else {
return c
}
}