-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathexercises.js
More file actions
102 lines (100 loc) · 3.06 KB
/
exercises.js
File metadata and controls
102 lines (100 loc) · 3.06 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
/** Function: firstReverse
* The function will take the str parameter being passed in and
* return the string in reversed order
* @param {string} str
* @return {string} reversed
* ie: "cat" => "tac"
*/
function firstReverse(str){
if (typeof str === 'string') {
return str.split('').reverse().join('')
} else {
return null;
}
}
/** Function: alphaOrder
* The function will take the str parameter being passed in and
* return the string in alphabetical order
* @param {string} str
* @return {string} in alphabetical order
* ie: "cake" => "acek"
*/
function alphaOrder(str){
if (typeof str === 'string'){
return str.split('').sort().join('');
} else {
return null;
}
}
/** Function: vowelCount
* The function will take the str parameter being passed in and
* return the number of vowels in the string
* @param {string} str
* @return {number} count of vowels
* ie: "oreo" => 3
*/
function vowelCount(str){
var vowels = ['a', 'e', 'i', 'o', 'u'];
if(typeof str === 'string'){
return str.split('').reduce((acc, curr) => {
(vowels.indexOf(curr.toLowerCase()) !== -1) ? acc++ : acc;
return acc;
}, 0)
} else {
return null;
}
}
/** Function: timeConvert
* The function will take the str parameter representing the amount of minutes being passed in and
* return the number of hours and minutes. Seperate the number of hours
* and minutes with a colon
* @param {number} str
* @return {string} as hours:minutes
* ie: 68 => 1:8
*/
function timeConvert(str){
if(typeof str === 'number'){
return `${(str / 60).toFixed(0)}:${str % 60}`;
} else {
return null;
}
}
/** Function: repeatString
* The function will take in two parameters and repeat a given string (first argument)
* num times (second argument). Return an empty string if num is a negative number
* @param {string} str
* @param {num} times
* @return {string} repeated num times
* i.e repeatString("money", 3) => "moneymoneymoney".
*/
function repeatString(str, times){
if(typeof str === 'string' && typeof times === 'number'){
if(times > 0){
return str.repeat(times);
} else {
return '';
}
} else {
return null;
}
}
/**
* Below here we see a module.exports which is set to an object with a bunch of keys.
* The module.exports syntax is a built-in javascript keyword that
* allows functionality from this file to be used in other files.
* Here we are exporting an object with a bunch of keys that will reference
* the functions you have made. After you are done implementing a function,
* change the 'null' to reference the corresponding function you have just created.
* Then go into your terminal and run 'npm test' to see if the tests pass for your function.
*
* The reason why we export our functions out of this file is because behind the scenes,
* the test-runner is importing this file so that it can gain access to the functions
* and run tests on it.
*/
module.exports = {
firstReverse: firstReverse,
alphaOrder: alphaOrder,
vowelCount: vowelCount,
timeConvert: timeConvert,
repeatString: repeatString
}