Last active
December 13, 2021 18:42
-
-
Save vczb/69d7d1a0aedf8932f71e19fe2db69228 to your computer and use it in GitHub Desktop.
random list of palindromic words
This file contains hidden or 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
| /* | |
| 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: You'll need to | |
| return just the number of words. | |
| */ | |
| const alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; | |
| const charLimit = [3,4,5]; | |
| const randomWords = []; | |
| const palindromeWords = []; | |
| let i = 0; | |
| while (i <= 10_000){ | |
| const word = alphabet.sort(function(a, b){return 0.5 - Math.random()}).join('').slice(0,charLimit.sort(function(a, b){return 0.5 - Math.random()})[0]); | |
| randomWords.push(word); | |
| const palindrome = word.split("").reverse().join(''); | |
| if(palindrome === word){ | |
| palindromeWords.push(word); | |
| } | |
| i++; | |
| } | |
| console.log(`palindromes words ${palindromeWords.length}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment