questionID 変数を使用して、いくつかの質問と回答を追跡しようとしています。しかし、関数を実行するたびに、変数がメモリを失うという問題があるようです。とにかくそう思う。
色を尋ねてから、ピンク、紫、または緑の答えをフォローアップすると、最後のステートメントにジャンプします。else if(questionID == -1)
これは私の意図ではありません。
この動作を修正するにはどうすればよいですか? スクリプトをローカルで実行します。
var questionID = -1;
function askQ() {
var findme = document.getElementById('askBox').value.toLowerCase();
var dontUnderstand = new Array();
dontUnderstand[0] = "Excuse me, What did You say ?";
dontUnderstand[1] = "Sorry, I cant understand You now. ^_^";
dontUnderstand[2] = "Sorry!, I got to go now... CU !";
dontUnderstand[3] = "ohh... Can you repeat that one more time ?";
var randomIndex = Math.round(Math.random() * 3);
if (findme.match(/(age|old)/)) {
document.getElementById('bubble').innerHTML = "ohh,, I am " +
obj1.age + " years old, How old are You ?";
}
if (!isNaN(findme)) {
document.getElementById('bubble').innerHTML = "ohh.. that's kinda old !";
}
if (findme.match(/(color)/)) {
document.getElementById('bubble').innerHTML = "My favourite color " +
" is Purple, What's yours ?";
questionID = 6;
}
if (findme.match(/(pink|purple|green)/) && questionID == 6) {
document.getElementById('bubble').innerHTML = "well...that's cute !!";
questionID = -1;
}
if (findme.match(/(hi|hello)/)) {
document.getElementById('bubble').innerHTML = "Hi there, nice to meet You !";
} else if (questionID == -1) {
document.getElementById('bubble').innerHTML = dontUnderstand[randomIndex];
}
}