Last active
October 10, 2018 20:47
-
-
Save SavvasStephanides/d7446100d767671a65c7088035299946 to your computer and use it in GitHub Desktop.
NodeJS code to display the longest acceptable word that can be written on a seven-segment display.
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
var fileSystem = require("fs"); | |
displayLongestAcceptableWord(); | |
function displayLongestAcceptableWord(){ | |
var longestAcceptableWords = getLongestAcceptableWords(); | |
longestAcceptableWords.forEach(function(word){ | |
console.log(word); | |
}); | |
} | |
function getLongestAcceptableWords(){ | |
var wordList = getWordList(); | |
var acceptableWordList = getAcceptableWordsFromList(wordList); | |
var longestAcceptableWord = getLongestWordsFromList(acceptableWordList); | |
return longestAcceptableWord; | |
} | |
function getWordList(){ | |
var fileContent = fileSystem.readFileSync("words.txt", "utf-8"); | |
return fileContent.split("\n"); | |
} | |
function getAcceptableWordsFromList(wordList){ | |
var acceptableWords = []; | |
wordList.forEach(function(eachWord){ | |
addWordToListIfAcceptable(eachWord, acceptableWords); | |
}); | |
return acceptableWords; | |
} | |
function addWordToListIfAcceptable(word, wordList){ | |
if(wordIsAcceptable(word)){ | |
wordList.push(word); | |
} | |
} | |
function wordIsAcceptable(word){ | |
var forbiddenWordPattern = /[gkmqtvwxyz0-9-]/; | |
return !word.match(forbiddenWordPattern); | |
} | |
function getLongestWordsFromList(wordList){ | |
var longestWord = [""]; | |
wordList.forEach(function(word){ | |
if(word.length > longestWord[0].length){ | |
longestWord = [word]; | |
} | |
else if(word.length == longestWord[0].length){ | |
longestWord.push(word); | |
} | |
}); | |
return longestWord; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment