JS Algo tests
Last active
September 1, 2020 22:16
-
-
Save GGrassiant/d7b40c0b9931cf32a9f590d99bcffb27 to your computer and use it in GitHub Desktop.
Algo tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isConsonant(letter) { | |
return !letter.match(/[aeiouy\s]/i); | |
} | |
function insertDash(word) { | |
// TODO: implement the method and return word with dashes | |
if (!word || word.length === 0) { | |
return ""; | |
} | |
const chars = word.split(''); | |
const newChars = chars.map((letter, i, letters) => { | |
if (i > 0 && isConsonant(letters[i - 1]) && isConsonant(letter)) { | |
return `-${letter}`; | |
} | |
return letter; | |
}); | |
return newChars.join(""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fizzBuzz = n => { | |
let i; | |
for (i=1; i<n ; i++) { | |
console.log( | |
(i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i | |
) | |
} | |
}; | |
// example: | |
fizzBuzz(100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function occurrences(text, word) { | |
if (!text || text.length === 0) { | |
return 0; | |
} | |
if (!word || word.length === 0) { | |
return 0; | |
} | |
const wordsInText = text.split(' '); | |
const reducer = (count, cWord) => count + (cWord.toLowerCase() === word.toLowerCase() ? 1 : 0); | |
const occurrencesCount = wordsInText.reduce(reducer, 0); | |
return occurrencesCount; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Counter { | |
constructor(text) { | |
// TODO: build an internal Map of word => occurrences. | |
this.mapOfWords = new Map(); | |
if (text && text.length > 0) { | |
text.split(' ').forEach((word) => { | |
this.mapOfWords.set(word.toLowerCase(), (this.mapOfWords.get(word.toLowerCase()) || 0) + 1); | |
}); | |
} | |
} | |
occurrences(word) { | |
// TODO: return the number of occurrences | |
return this.mapOfWords.get(word.toLowerCase()) || 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment