forked from sf-wdi-27-28/function-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
72 lines (52 loc) · 1.65 KB
/
functions.js
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
// YOUR CODE HERE
//MaxOrMin
function maxOrMin(num1, num2, max) {
var sorted = [num1, num2].sort(function(a, b){
return a - b;
});
if(max) {
return sorted [1];
} else {
return sorted[0];
}
}
//Silly Sum
// Write a function that takes an array of numbers and returns the sum of each number multiplied by its index.
var sillySum = function(arr) {
var result = 0;
for (var i= 0; i < arr.length; i+= 1) {
result += arr[i] * i;
}
return result;
};
var myArray= [3, 9, 29, 32];
console.log(sillySum(myArray));
//is Prime
// Create a function that returns true if the number passed in is a prime number and false if not.
var isPrime = function(num) {
if (num < 2) {
return false;
}
for (var i = 2; i < num; i +=1) {
if (num % i === 0) {
return false;
}
}
return true;
};
//filter
//Write a function called filter that filters elements of an array based on a callback function.
//filter takes in an array and another function (a callback). The callback for filter will take in a number and return true or false (like isPrime does!). Your filter function should create a new array. The filter function should use the callback on each element of the original array. When the callback returns true for an element, filter should add that element to the new array. Make filter return the new array at the end!
//starter structure for our filter function
var filter = function(arr, callback) {
var newArr = [];
for(var i=0; i<arr.length; i++) {
if (callback(arr[i])){
newArr.push(arr[i]);
}
}
return newArr;
}
// you could also try using your isPrime function as the callback:
filter ([8,6,7,5,3,0,9], isPrime);
//returns [7,5,3]