-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.js
More file actions
115 lines (98 loc) · 2.88 KB
/
loops.js
File metadata and controls
115 lines (98 loc) · 2.88 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
// Iteration
// WHILE
var ourArray = [];
var i = 0;
while(i < 5) {
ourArray.push(i);
i++;
}
// FOR
/* For loops are declared with three optional expressions separated by semicolons: for (a; b; c),
where a is the intialization statement, b is the condition statement, and c is the final expression. */
var ourArray = [];
for (var i = 0; i < 5; i++) {
ourArray.push(i);
} //ourArray will now have the value [0,1,2,3,4]
// Iterate through an array
var arr = [10, 9, 8, 7, 6];
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// Nesting FOR loops
var arr = [
[1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
// DO {} WHILE () loop will execute once before checking the condition
// and WHILE (){} will only start executing if the condition is met in the first place
// Recursion
// Fuction that multiplies first n elements of an array and returns the product of this multiplication
function multiply(arr, n) {
if (n <= 0) { // base case employed if no recursion has happened or used in recursion
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1]; // n-1 because count is between 1st [0] and nth [n-1] elements
}
}
// PRACTICE
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
}
];
/* The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property. */
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i++) {
if (name == contacts[i]["firstName"]) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
// Recursion + a ? b : c conditionals
function countdown(n){
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1); //variable cannot be changed
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(3));
function rangeOfNumbers(startNum, endNum) {
return startNum === endNum
? [startNum]
: rangeOfNumbers(startNum, endNum - 1).concat(endNum); //merges two or more arrays
};
console.log(rangeOfNumbers(1,7));