Skip to content

Commit 2cf9e98

Browse files
committed
rest and spread operators lessons done
1 parent bc7a8ac commit 2cf9e98

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

app/index.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1-
//Default Funciton Paramaeters
1+
//Rest and Spread
22

3-
function makeAjaxRequest(url, method) {
3+
function addNums(numbers) {
4+
return numbers.reduce((total, number) => {
5+
return total + number;
6+
},0)
7+
}
48

5-
if (!method) {
6-
method = 'Get';
7-
}
9+
const a = [1, 2, 3, 4, 5, 6, 7];
810

9-
//logic for request
11+
console.log(addNums(a));
1012

11-
}
13+
//this says we don't know how many arguments are coming in, we want to capture them all and put them into
14+
//an array the ... is a rest operator that allows us to do this.
1215

13-
makeAjaxRequest('google.com');
14-
makeAjaxRequest('goole.com', 'GET');
16+
function addNums1(...numbers) {
17+
return numbers.reduce((total, number) => {
18+
return total + number;
19+
},0)
20+
}
1521

1622

17-
//This says if user did not pass in a method argument, it is assgined 'GET' otherwise method is whatever is sent in.
18-
//if you want it to be undefined specifically you can pass in null instead.
19-
function makeAjaxRequest(url, method = 'GET') {
23+
console.log(addNums1(1,2,3,4,5,6,7));
2024

21-
//logic for request
2225

23-
}
26+
const defaultColors = ['red', 'green'];
27+
const userFavColors = ['orange', 'yellow'];
2428

25-
makeAjaxRequest('google.com');
26-
makeAjaxRequest('google.com', 'POST');
27-
makeAjaxRequest('google.com', null);
29+
console.log(defaultColors.concat(userFavColors));
2830

31+
//this is the spread operator, a new array was created with the [] brackets, we then put inside of it, a reference
32+
//to existing arrays defaultColors and userFavColors we then added the spread operator which is the ... which means
33+
//we want to take all of what is in the referenced array and pull them out into the new array. The same process is
34+
//repeated with all spread operators followed by an array. It removes whatever was in the referenced array and puts it
35+
//into the new array.
36+
console.log([...defaultColors, ...userFavColors]);
37+
//can also add in new elements at the same time...
38+
console.log(['blue',...defaultColors, 'green', ...userFavColors]);
2939

30-
//__________________________
3140

32-
function User(id) {
33-
this.id = id;
41+
//here we are using both rest and spread. First a list of items of any size is sent in. We check to see
42+
//if there is milk on the list, if not, we add it to the list of items and return it. Otherwise we just return the list.
43+
function validateShoppingList(...items) {
44+
if (items.indexOf('milk') < 0) {
45+
return ['milk', ...items];
46+
}
47+
return items;
3448
}
3549

36-
function generateID() {
37-
return Math.random() * 999999;
38-
}
39-
//this makes use of default arguements, if a user isn't passed in, it generates a user to make admin. So can
40-
//work either way as is called below;
41-
function createAdminUser(user = new User(generateID())) {
42-
user.admin = true;
43-
return user;
44-
}
50+
console.log(validateShoppingList('oranges', 'bread', 'eggs'));
4551

46-
createAdminUser();
47-
const user = new User(generateID());
48-
createAdminUser(user);
52+
53+
const MathLibrary = {
54+
calcProduct (...rest) {
55+
console.log('please use multNums instead');
56+
return this.multNums(...rest)
57+
},
58+
multNums(a,b) {
59+
return a*b;
60+
}
61+
};

0 commit comments

Comments
 (0)