I'm using node.js to create an IRC bot that asks trivia questions. So far it works great, but I wanted to implement a system that generates hints by providing you with underscores for each letter. As time passes, it slowly fills in the blanks. If nobody gets the answer before all the blanks are filled in, the bot moves on. (If it is not in single-question mode)
The issue: If nobody answers the question, the bot moves on to the next one as intended. The bot then supplies two hints at the same time. Sometimes it just repeats the first hint, but other times it's actually satisfying the else
statement and providing the next hint.
I have troubleshooted to verify that the function getHint
is only being called ONCE.
I've been staring at this code for nearly 2.5 hours now and I'm beginning to lose hope. I'm brand new to javascript, this is essentially my first time coding in it. Any help is GREATLY appreciated.
EXAMPLE 1: Satisfies if (there is no hint)
twice
BOT> Who was the drummer for The Beatles?
BOT> _ _ _ _ _ _ _ _ _ _
BOT> R _ _ G O S _ A _ R
BOT> R _ N G O S T A R R
BOT> Oh man, nobody got the answer! It was: RINGO STARR
BOT> Who is the president of the United States?
BOT> _ _ _ _ _ _ _ _ _ _ _ **
BOT> _ _ _ _ _ _ _ _ _ _ _ ** THESE TWO MESSAGES ARE SENT AT THE SAME TIME
EXAMPLE 2: Satisfies if (there is no hint)
AND the corresponding else
statement
BOT> Who was the drummer for The Beatles?
BOT> _ _ _ _ _ _ _ _ _ _
BOT> R _ _ G O S _ A _ R
BOT> R _ N G O S T A R R
BOT> Oh man, nobody got the answer! It was: RINGO STARR
BOT> Who is the president of the United States?
BOT> _ _ _ _ _ _ _ _ _ _ _ **
BOT> _ A _ A C _ O B _ _ _ ** THESE TWO MESSAGES ARE SENT AT THE SAME TIME
I've done troubleshooting to follow the bot's path. giveHint()
is only being called once.
function giveHint() {
if (triviaActive) { //If trivia is enabled
if (!isHint) { //If there's no hint
isHint = true;
triviaHint = triviaAnswer.replace(/ /g, ' ');
triviaHint = triviaHint.replace(/[A-Za-z0-9]/g, '_ ');
modAnswer = triviaAnswer.replace(/[^A-Za-z0-9]/g, '');
answerArray = modAnswer.split('');
totalLetters = Math.round(answerArray.length / 2);
bot.say(to, triviaHint);
giveHintInterval = setInterval(giveHint, 11000);
} else { //There is already a hint
for (var i = 0; i < totalLetters - 1; i++) {
giveLetter = Math.floor(Math.random() * answerArray.length);
characterReplace = answerArray[giveLetter];
answerArray.splice(giveLetter, 1);
triviaHint = replaceNthMatch(triviaHint, "_", giveLetter + 1, characterReplace);
console.log("Replacing the " + giveLetter + " underscore with " + characterReplace);
if (answerArray.length == 0) {
i = totalLetters; //Escape loop
clearInterval(giveHintInterval);
if (!triviaCont) { //If trivia is 'single question' mode
isHint = false;
triviaActive = false;
bot.say(to, "Oh man, nobody got the answer! It was: " + color(triviaFull, 6));
triviaAnswer = "";
} else { //If trivia is in 'continuous mode'
isHint = false;
triviaActive = false;
bot.say(to, "Oh man, nobody got the answer! It was: " + color(triviaFull, 6));
triviaAnswer = "";
doTrivia(); //Ask a new question
}
}
}
bot.say(to, triviaHint);
}
}
}
doTrivia() - *This function finds a random song from a database of songs and asks a question about it
function doTrivia() {
if (!triviaRestricted) {
//New question, restart the hint timer
if (giveHintInterval) {
clearInterval(giveHintInterval);
}
for (var i=0;i!=1;i) {
getRandomLine('shuffle.txt');
var shufflearray = shuffled.split(",");
var submitter = shufflearray[0];
var shufArtist = shufflearray[1];
var shufTitle = shufflearray[2];
var shufLink = shufflearray[3];
if (shufArtist && shufTitle) {
triviaActive = true; //Trivia is now active
i=1; // escape loop
var max = 2;
var min = 1;
var trivRandom = Math.floor(Math.random()*(max-min+1)+min);
isHint = false;
if (trivRandom == 1) {
triviaQuestion = "Who is the artist of "+color(shufTitle,12)+"?";
bot.say(to,triviaQuestion);
triviaAnswer = shufArtist;
triviaFull = shufArtist+" - "+shufTitle;
giveHint();
} else {
triviaQuestion = "Can you name this song by "+color(shufArtist,12)+"?";
triviaFull = shufArtist+" - "+shufTitle;
triviaAnswer = shufTitle;
bot.say(to, triviaQuestion);
giveHint();
}
}
}
} else {bot.notice(from, "Trivia is currently disabled.");}
}
I have found the answer to my question. In case anyone was curious, the issue was a recursive function. giveHint was calling doTrivia which calls giveHint. Thanks.