-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkForCaps.js
52 lines (33 loc) · 1.33 KB
/
checkForCaps.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
//Efficient solution
var detectCapitalUse = function(word) {
return word === word.toUpperCase() || word === word[0] + word.substr(1).toLowerCase();
};
//brute force
function capitals(str){
//str.split
word = word.split('')
let length = word.length -1
let penUltIndex = word.length - 2
//nested for loops
if(word.length === 1 && word[0] === word[0].toUpperCase()){
return true
}else if (word[length] === word[length].toUpperCase() && word[penUltIndex] === word[penUltIndex].toLowerCase() ){
return false
}
for( i = 0; i < word.length; i++){
// if a letter is uppercase, and it its index ! 0
for( j = i+1; j < word.length -1; j++){
//conditional logic
if(word[j] === word[j].toUpperCase()){
console.log(word[j+1])
//check if the index i-1 & i+1 are uppercase
if(word[j-1] === word[j-1].toLowerCase() || word[j+1] === word[j+1].toLowerCase()){
//if not, return false
return false
}
}
}
}
//else return true
return true
};