-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex05.js
64 lines (36 loc) · 1.19 KB
/
ex05.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
// FUNCTION SPLIT
function split(str, separator){
const words = [] // contains the final words
let nWord = 0
for (i = 0; i < str.length; i++){ // i++ = increment of 1
// words[nWord] += str[i]
if (words[nWord] === undefined){
words[nWord] = str[i]
}else{
words[nWord] += str[i]
}
if (str[i] === separator){
nWord = nWord + 1
}
console.log(str[i],words)
}
return words // returns the array with the substrings we built
}
// FUNCTION DECAPITALIZE
function decapitalize(str){
const words = split(str, ' ')
let sentence = ''
for (let i = 0; i < words.length; i++){ // loop on words
for (let x = 0; x < words[i].length; i++){ // walk in the string
if (x === 0 && words[i].charCodeAt(x) >= 65 && words[i].charCodeAt(x) <= 90){
sentence += String.fromCharCode(words[i].charCodeAt(x) + 32)
}
else{
sentence += String.fromCharCode(words[i].charCodeAt(x))
}
}
}
return sentence
}
console.log(decapitalize('Wooman'))
console.log(decapitalize('El Drago'))