-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.js
More file actions
99 lines (79 loc) · 1.75 KB
/
arrays.js
File metadata and controls
99 lines (79 loc) · 1.75 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
// printReverse Function
// prints all values in an array in reverse order
function printReverse(numbersArr){
var nums = numbersArr.length -1;
for(i = nums; i >= 0; i--){
console.log(numbersArr[i]);
}
}
//isUniform Function
//compares values in array for equal values
function isUniform(valueArray){
var first = valueArray[0];
for(i = 1; i < valueArray.length; i++){
if(valueArray[i] !== first){
return false;
}
}
return true;
}
//sumArray function
//add all numbers in an array
function sumArray(sumNumbers){
var total = 0;
//for loop
// for(i=0; i< sumNumbers.length; i++){
// total += sumNumbers[i];
// }
// return total;
//foreach loop
sumNumbers.forEach(function(element){
total += element;
});
return total;
}
//max function
//return largest number in array
function max(numbers){
var largest = numbers[0];
for(i=1; i < numbers.length; i++){;
if(numbers[i] > largest){
largest = numbers[i];
}
}
return largest;
}
//myForEach
//handcoded function to perform forEach loops on Arrays
function myForEach(arr, func){
//l
for( var i = 0; i < arr.length; i++){
func(arr[i]);
}
}
//function movieDataPrint
//cycles through array, prints information on each movie within it.
//sample variable with "database"
var movies = [
{
"title" : "The Matrix",
"rating" : 4.5,
"hasWatched" : true},
{
"title" : "The Matrix 2",
"rating" : 5.5,
"hasWatched" : false},
{
"title" : "The Notebook",
"rating" : 6,
"hasWatched" : false}
];
function movieDataPrint(arr){
arr.forEach(function(element){
if(element.hasWatched){
console.log("You have watched " + element.title + " which is rated " + element.rating);
} else {
console.log("You have not watched " + element.title + " which is rated " + element.rating);
}}
);
}