-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpart1.js
More file actions
69 lines (51 loc) · 2.14 KB
/
Copy pathpart1.js
File metadata and controls
69 lines (51 loc) · 2.14 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
/* =====================
# Lab 1, Part 1 — Function Review
===================== */
/* =====================
Instructions: "Write a function that adds one to the number provided"
Example: "plusOne(2) should return 3"
===================== */
var plusOne = function (num) {return num + 1};
console.log('plusOne success:', plusOne(99) === 100);
/* =====================
Instructions: "Write a function, age, that takes a birth year and returns an age in years."
(Let's just assume this person was born January 1 at 12:01 AM)
Example: "age(2000) should return 18"
===================== */
var age = function(birth) {return 2017-birth};
console.log('age success:', age(1971) === 46);
/* =====================
Instructions: "Write a function that returns true for numbers over 9000 and false otherwise"
Example: "over9000(22) should return false"
===================== */
var over9000 = function(number) {
if (number > 9000) {return true}
else {return false}
};
console.log('over9000 success:', over9000(9001) === true && over9000(12) === false);
/* =====================
Instructions: "Write a function that returns the value of an object at a specified key"
Example: "valueAtKey({'name': 'Nathan'}, 'name') should return 'Nathan'"
===================== */
var valueAtKey = function(obj,b) {return obj[b]};
console.log('valueAtKey success:', valueAtKey({'foo': 'bar'}, 'foo') === 'bar');
/* =====================
Instructions: "Write a function which returns the y coordinate of a line given m, x, and b"
Example: "y(0, 0, 0) should return 0; y(1, 1, 1) should return 2"
Remember: The standard mathematical expression for such a function is y=mx+b
===================== */
var y = function(m,x,b) {return m*x+b};
console.log('y success:', y(12, 1, 12) === 24);
/* =====================
Instructions: "Write a function which counts the number of times a value occurs in an array "
Example: "countItem(['a', 'b', 'a'], 'a') should return 2"
===================== */
var countItem = function(a,b) {
let x=0;
for (var i=0; i<a.length;i++){
if (a[i]==b){x=x+1}
else{}
}
return x
};
console.log('countItem success:', countItem([1, 2, 3, 4, 5, 4, 4], 4) === 3);