Skip to content

Instantly share code, notes, and snippets.

@chrisbward
Created October 28, 2020 14:16
Show Gist options
  • Save chrisbward/7b40147956db748196738f5efd441df5 to your computer and use it in GitHub Desktop.
Save chrisbward/7b40147956db748196738f5efd441df5 to your computer and use it in GitHub Desktop.
Interview Exercise
/*
Description:
Write a script that creates an array with 10000 random words between 3 and 5 characters, and returns the number of words that are palindromes in that array;
Notes:
The code needs to be in javascript
You’ll need to return just the number of words
You have 30 mins, if you run out of time, submit the form anyway.
Once you click “Start” you cannot pause the countdown
Don’t delete the main function
const main = () => {
}
*/
const isPalindrome = (wordToTest) => {
const reversedString = wordToTest.split("").reverse().join("");
return reversedString === wordToTest;
};
const generateWords = (numberOfWordsNeeded) => {
let wordsArray = [];
for (let iLoopA = 0; iLoopA < numberOfWordsNeeded; iLoopA++) {
wordsArray.push(Math.random().toString(36).substr(2, Math.floor(Math.random() * 3) + 3));
}
return wordsArray;
};
const main = (wordsNeededCount) => {
let countedPalindromes = 0;
generateWords(wordsNeededCount).forEach((wordToTest) => {
if (isPalindrome(wordToTest)) {
countedPalindromes++;
}
});
console.log("number of palindomes: " + countedPalindromes);
};
main(10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment