-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (47 loc) · 1.33 KB
/
index.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
//Coderbyte Searching Challenge in JavaScript
function SearchingChallenge(str) {
const words = str.split(" ");
let maxRepeatedLetters = 0;
let wordWithMaxRepeatedLetters = "";
for (let i = 0; i < words.length; i++) {
const word = words[i];
const letterCounts = {};
let repeatedLetters = 0;
for (let j = 0; j < word.length; j++) {
const letter = word[j].toLowerCase(); // Convert letter to lowercase for case-insensitive comparison
if (/[a-z]/i.test(letter)) {
if (letter in letterCounts) {
letterCounts[letter]++;
if (letterCounts[letter] === 2) {
repeatedLetters++;
}
} else {
letterCounts[letter] = 1;
}
}
}
if (repeatedLetters > maxRepeatedLetters) {
maxRepeatedLetters = repeatedLetters;
wordWithMaxRepeatedLetters = word;
}
}
if (maxRepeatedLetters === 0) {
return -1;
} else {
let finalOutput = wordWithMaxRepeatedLetters
.split("")
.filter((char) => !/i|q|b|o|l|p|g|3|j|1|7|4/i.test(char))
.join("");
if (finalOutput === "") {
return "EMPTY";
} else {
return finalOutput;
}
}
}
console.log(SearchingChallenge("Hello apple pie"));
// Output: Hello
// Final Output: He
console.log(SearchingChallenge("No words"));
// Output: -1
// Final Output: -