-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-03-19.js
64 lines (58 loc) · 1.56 KB
/
2023-03-19.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
/* Kata 1 */
/* -------------------------------------- */
/* my solution */
function findNeedle( haystack ) {
for ( let i = 0; i < haystack.length; i++ ) {
if ( haystack[i] === 'needle' ) {
return `found the needle at position ${i}`;
}
}
}
console.log( findNeedle( ['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'] ) );
/* optimized */
function findNeedle2( haystack ) {
return `found the needle at position ${haystack.indexOf( 'needle' )}`;
}
console.log( findNeedle2( ['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'] ) );
/* Kata 2 */
/* -------------------------------------- */
/* my solution */
function findAverage( array ) {
if ( array.length === 0 ) {
return 0;
} else {
let sum = 0;
let avg = 0;
for ( let i=0; i<array.length; i++ ) {
sum += array[i];
}
avg = sum / array.length;
return avg;
}
}
console.log( findAverage( [5, 6, 9, 1, 14, 15, 31, 22, 38, 4] ) );
/* Kata 3 */
/* -------------------------------------- */
/* my solution */
function bmi( weight, height ) {
const bmi = weight / height**2;
if ( bmi <= 18.5 ) {
return 'Underweight';
} else if ( bmi <= 25.0 ) {
return 'Normal';
} else if ( bmi <= 30.0 ) {
return 'Overweight';
} else if ( bmi > 3 ) {
return 'Obese';
}
}
console.log( bmi( 62, 175 ) );
/* optimized */
function bmi2( weight, height ) {
const bmi = weight / height**2;
if ( bmi <= 18.5 ) return 'Underweight';
else if ( bmi <= 25.0 ) return 'Normal';
else if ( bmi <= 30.0 ) return 'Overweight';
else if ( bmi > 30 ) return 'Obese';
}
console.log( bmi2( 62, 175 ) );