-
Notifications
You must be signed in to change notification settings - Fork 307
/
assessment.js
102 lines (84 loc) · 2.87 KB
/
assessment.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
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
/*
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!'.
function helloWorld() {
return 'Hello World!';
}
/*
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
*/
function lambdaSchool(num) {
if (num % 5 === 0 && num % 3 === 0) {
return 'Lambda School';
} else if (num % 3 === 0) {
return 'Lambda';
} else if (num % 5 === 0) {
return 'School'
}
return 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'
*/
function longestString(strs) {
let longest = '';
strs.forEach((str) => {
const strLength = str.length;
const longestLength = longest.length;
if (strLength > longestLength) {
longest = str;
}
});
return longest;
}
/*
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)
*/
function computeUserAverageAge(users) {
let ageSum = 0;
Object.values(users).forEach((user) => {
ageSum += user.age;
});
const totalUsers = Object.keys(users).length;
const roundedAverage = Math.round(ageSum / totalUsers);
return roundedAverage;
}
module.exports = {
helloWorld,
lambdaSchool,
longestString,
computeUserAverageAge
};