forked from seanwessmith/Lambda-Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment.js
More file actions
133 lines (103 loc) · 3.33 KB
/
assessment.js
File metadata and controls
133 lines (103 loc) · 3.33 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Work through the problems in this file. As you work through each problem periodically run: npm test
Your function name and the string must match the instructions exactly otherwise the tests will fail.
After writing your function uncomment the matching function reference at the bottom of the file.
*/
// 1. Write a function called helloWorld that returns the string 'Hello World!'.
var a;
function helloWorld (a) {
return 'Hello World!';
}
helloWorld(a)
/*
2. Write a function called lambdaSchool that has a single parameter called num.
num will be a positive integer.
If num is divisible by 3 return the string 'Lambda'
If num is divisible by 5 return the string 'School'
If num is divisible by 3 AND 5 return the string 'Lambda School' (notice the space)
If num is NOT divisible by 3 or 5 then return num.
Example:
lambdaSchool(15); // returns 'Lambda School'
lambdaSchool(8); // returns 8
*/
var num = (Math.floor(Math.random()*50)) + 1
function lambdaSchool (num) {
if (num%3 === 0 && num%5 === 0) {
return 'Lambda School'
}
else if (num%3 === 0 && num%5 !== 0) {
return'Lambda'
}
else if (num%3 !== 0 && num%5 ===0){
return 'School'
}
else {
console.log(num)
}
return num;
}
//lambdaSchool(num)
/*
3. Write a function called longestString that has a single parameter called strs.
strs will be an array of strings.
Return the longest string that is in strs.
If there is a tie for the longest string then return the one that occurs first.
Example:
longestString(['hi', 'hello', 'ni hao', 'guten tag']); // returns 'guten tag'
longestString(['JavaScript', 'HTML', 'CSS']); // returns 'JavaScript'
*/
//var strs = ['one', 'two','three','four','five','six','seven'];
var strs = [];
function longestString(strs){
var length = 0;
var longest;
for (var i=0; i<strs.length; i++){
if (strs[i].length > length){
length = strs[i].length;
longest = strs[i];
}
}
return longest;
}
longestString(strs)
/*
4. Write a function called computeUserAverageAge that has a single parameter called users
users is an array of user objects.
Each user object has a property called age that is a number.
Compute the average age of all user objects in the users array.
Round off the decimals if needed and return the number.
Example:
const users = [{
name: 'Brendan Eich',
age: 56,
}, {
name: 'Linus Torvalds',
age: 48,
}, {
name: 'Margaret Hamilton',
age: 81,
}];
computeUserAverageAge(users); // returns 62 (This number is rounded up from 61.6666)
*/
const users = [];
function computeUserAverageAge(users) {
const addens = [];
for (var i = 0; i < users.length; i++){
addens.push(users[i].age)
}
const total = addens.reduce(function(acc, item){return acc + item;});
const average = total / addens.length;
const roundAverage = Math.round(average);
//console.log(addens);
//console.log(total);
//console.log(average);
//console.log(roundAverage);
return roundAverage
}
//computeUserAverageAge(users);
module.exports = {
helloWorld,
lambdaSchool,
longestString,
computeUserAverageAge
};