Reputation: 5
No result is being produced by the codes below. I'm trying to assign a random item from an array to the constant called palindromeGenerator, and this word will be evaluated as a palindrome word or not (for which I'm yet to figure out the codes).
const palindromeGenerator = (random) => {
var palindromeArray = [
"racecar", "noon", "mom", "tenet", "sagas",
"hill", "programs", "computer", "internet"
];
var random = Math.floor(Math.random() * 10);
if (random == 0) { palindromeArray[0]; };
else if (random == 1) { palindromeArray[1]; };
else if (random == 2) { palindromeArray[2]; };
else if (random == 3) { palindromeArray[3]; };
else if (random == 4) { palindromeArray[4]; };
else if (random == 5) { palindromeArray[5]; };
else if (random == 6) { palindromeArray[6]; };
else if (random == 7) { palindromeArray[7]; };
else if (random == 8) { palindromeArray[8]; };
else { palindromeArray[9]; };
}
palindromeGenerator;
Upvotes: 0
Views: 32
Reputation: 207501
You do not return anything so nothing happens with the function call. You are also not calling the function. You have abunch of if statements to get the index when all you need to do is use the number generated.
var palindromeArray = ["racecar", "noon", "mom", "tenet", "sagas", "hill", "programs", "computer", "internet"];
const palindromeGenerator = () => {
const random = Math.floor(Math.random() * palindromeArray.length);
return palindromeArray[random];
}
var pal = palindromeGenerator();
console.log(pal)
Upvotes: 1