Skip to content

Instantly share code, notes, and snippets.

@diegotauchert
Last active October 8, 2020 20:55
Show Gist options
  • Select an option

  • Save diegotauchert/d9b7248b7de4053edf28b44f8731f8a5 to your computer and use it in GitHub Desktop.

Select an option

Save diegotauchert/d9b7248b7de4053edf28b44f8731f8a5 to your computer and use it in GitHub Desktop.
Script that creates an array with 10000 random words between 3 and 5 characteres, and returns the words that are palindromes in that array
const main = () => {
var characters = 'abcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
var words = [];
var wordsPalindromes = [];
for(var j = 0; j < 10000; j++){
var wordLength = Math.floor(Math.random() * (5 - 3 + 1) ) + 3;
var word = '';
for ( var i = 0; i < wordLength; i++ ) {
word += characters.charAt(Math.floor(Math.random() * charactersLength));
}
words.push(word);
if(word.split('').reverse().join('') == word) {
wordsPalindromes.push(word);
}
}
return wordsPalindromes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment