Skip to content

Instantly share code, notes, and snippets.

@WikipediaBrown
Last active January 8, 2018 22:53
Show Gist options
  • Save WikipediaBrown/1eb0ed643b539c09822514997d7f5e80 to your computer and use it in GitHub Desktop.
Save WikipediaBrown/1eb0ed643b539c09822514997d7f5e80 to your computer and use it in GitHub Desktop.
isEven
// type you numbers you want to test and then type odne when you are done and this will read out the numbers wiht their boolean values indicating if they are even
const standardInputFromTheCommandLine = process.openStdin()
let arrayOfObjectsWithNumbersForKeysAndBooleansForValues = []
standardInputFromTheCommandLine.addListener('data', incomingData => {
if (incomingData.toString().trim() == 'done') {
process.stdin.emit("end")
}
let object = {}
let key = incomingData.toString().trim()
let value = isEven(incomingData)
object[key] = value
arrayOfObjectsWithNumbersForKeysAndBooleansForValues.push(object)
})
// This function adds an event listener that fires when the comand line is done taking input
standardInputFromTheCommandLine.addListener('end', () => {
console.log(arrayOfObjectsWithNumbersForKeysAndBooleansForValues)
})
// This function uses the modulus of the incoming number and 2 to return a boolean indicating if it is even
var isEven = function(incomingNumber) {
let number = Number(incomingNumber)
if (isNaN(number)) {return NaN}
return (number % 2) == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment