-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced-looping-solution.js
More file actions
84 lines (71 loc) · 1.63 KB
/
advanced-looping-solution.js
File metadata and controls
84 lines (71 loc) · 1.63 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
const basket = ['apples', 'oranges', 'grapes'];
const detailedBasket = {
apples: 5,
oranges: 10,
grapes: 1000
}
//1
for (let i = 0; i < basket.length; i++) {
console.log(basket[i]);
}
//2
basket.forEach(item => {
console.log(item);
})
for (item in detailedBasket) {
console.log(item);
}
for (item of basket) {
console.log(item);
}
// Question #1:
// create a function called biggestNumberInArray() that takes
// an array as a parameter and returns the biggest number.
// biggestNumberInArray([-1,0,3,100, 99, 2, 99]) should return 100;
// Use at least 3 different types of javascript loops to write this:
const array = [-1,0,3,100, 99, 2, 99] // should return 100
const array2 = ['a', 3, 4, 2] // should return 4
const array3 = [] // should return 0
function biggestNumberInArray(arr) {
let highest = 0;
for (let i = 0; i < arr.length; i++) {
if (highest < arr[i]) {
highest = arr[i];
}
}
return highest
}
function biggestNumberInArray2(arr) {
let highest = 0;
arr.forEach(item => {
if (highest < item) {
highest = item;
}
})
return highest;
}
function biggestNumberInArray3(arr) {
let highest = 0;
for (item of arr) {
if (highest < item) {
highest = item;
}
}
return highest;
}
biggestNumberInArray3(array3)
// Question #2:
// Write a function checkBasket() that lets you know if the item is in the basket or not
amazonBasket = {
glasses: 1,
books: 2,
floss: 100
}
function checkBasket(basket, lookingFor) {
for (item in basket) {
if (item === lookingFor) {
return `${lookingFor} is in your basket`
}
}
return 'that does not exist in your basket'
}