I'm trying to find out if a value exists in an array. The following code is giving me an error each time I run saying Object has no replace method.
var fruits = ['apples', 'pears', 'bananas'];
console.log("Enter in a fruit name");
process.stdin.on('data', function(fruit) {
fruit = fruit.replace("\n", "");
if (fruits.indexOf(fruit) >= 0 ) {
console.log("The value has been found in the array");
process.exit(); }
else {
console.log("Value not found");
process.exit(); }
});
At first it kept returning "Value not found" no matter what I entered, so I surmised it was the line break/enter that I press after entering my fruit. But the replace method for the fruit refuses to take. What am I missing?
fruit
? – 0x499602D2 Apr 23 at 23:03console.log(JSON.stringify(fruit))
to see what you are searching for. – Bergi Apr 23 at 23:04