-
Notifications
You must be signed in to change notification settings - Fork 0
/
repetitionChecker.js
37 lines (36 loc) · 1.32 KB
/
repetitionChecker.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
let inputString = "1 1 h"
// checkForRepetition(inputString)
checkForRepetition(inputString)
// const s = 'word word'
// console.log(returnWordIndex('bo',-7,s))
// console.log(returnWordIndex('word',-3,s))
// function checkForRepetition(inputString)
// {
// inputString = inputString.toLowerCase()
// const words = inputString.split(" ")
// let repetitionFlag = 0
// for(let index=0;index<words.length;index++)
// {
// if(words.indexOf(words[index]) !== words.lastIndexOf(words[index])){
// repetitionFlag ++
// }
// }
// if(repetitionFlag==0) console.log("No repeated words!")
// else console.log("Repeated Words Exist!")
// }
function returnWordIndex(word, position, self) {
return self.indexOf(word) === position
}
//Another method
function checkForRepetition(inputString)
{
inputString = inputString.toLowerCase()
const words = inputString.split(" ")
const uniqueWords = words.filter(returnWordIndex)
let repeatedWords = words.length - uniqueWords.length
if(repeatedWords==0)
{
console.log("No repeated words!")
}
else console.log(repeatedWords," repeated word(s)!")
}