-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweird-string-case.js
48 lines (44 loc) · 1.13 KB
/
weird-string-case.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
// WeIrD StRiNg CaSe
// https://www.codewars.com/kata/52b757663a95b11b3d00062d/train/javascript
// using:
// https://stackoverflow.com/questions/6211613/testing-whether-a-value-is-odd-or-even
function isEven(n) {
return n % 2 == 0;
}
function toWeirdCase(string){
var sentenceArr = [];
//TODO
// split it into words
var wordArr=string.split(' ');
for (var i = 0;i < wordArr.length;i++) {
//split each word into an array of letters
var arr=wordArr[i].split('');
//first [0 index] to upper case
arr[0] = arr[0].toUpperCase();
for (var j = 1;j<arr.length;j++) {
//is it even?
if (isEven(j)) {
arr[j] = arr[j].toUpperCase();
} else {
// odd
arr[j] = arr[j].toLowerCase();
}
}
// join the array into a word
var word=arr.join('');
// push the word into another array
sentenceArr.push(word);
console.log(sentenceArr);
}
string = sentenceArr.join(' ');
return string;
}
// for future reference if I want odd
/*
function isEven(n) {
return n % 2 == 0;
}
function isOdd(n) {
return Math.abs(n % 2) == 1;
}
*/