-
Notifications
You must be signed in to change notification settings - Fork 0
/
7kyu-alternateCapitalization.js
36 lines (29 loc) · 1.15 KB
/
7kyu-alternateCapitalization.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
/*Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.
For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See test cases for more examples.
The input will be a lowercase string with no spaces.
Good luck!*/
//P: one input, a string of lowercase letters
//R: return an array containing two strings, one with all even indices capitalized and one with all odd indices capitalized
//E: "abcdef" => ['AbCdEf', 'aBcDeF']
//P: declare a var for empty array to return the result
// declare a function to turn all even indices to caps, push to results array
// declare function to turn all odd indices to caps, push to results array
// return results array
function capitalize(s){
let results = [];
let evenStr = s.split('');
let oddStr = s.split('');
evenStr.forEach((el, i) => {
if(i%2===0){
evenStr[i] = el.toUpperCase()
}
})
oddStr.forEach((el, i) =>{
if(i%2===1){
oddStr[i] = el.toUpperCase()
}
})
results.push(evenStr.join(''))
results.push(oddStr.join(''))
return results;
}