Skip to content

Instantly share code, notes, and snippets.

@GGrassiant
Last active September 1, 2020 22:16
Show Gist options
  • Save GGrassiant/d7b40c0b9931cf32a9f590d99bcffb27 to your computer and use it in GitHub Desktop.
Save GGrassiant/d7b40c0b9931cf32a9f590d99bcffb27 to your computer and use it in GitHub Desktop.
Algo tests
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("");
}
const fizzBuzz = n => {
let i;
for (i=1; i<n ; i++) {
console.log(
(i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i
)
}
};
// example:
fizzBuzz(100);
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;
}
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