英文:
I am trying to evaluate if the input is an empty string or not a number in JavaScript
问题
我试图评估用户输入为空字符串(空字符串)或除数字以外的任何内容(非数字)时。在我控制台记录空字符串的输入后,返回了NaN。我不确定为什么else-if语句从未识别到,如果我测试空字符串或NaN值。这也最终影响了我的平均总分。
if (input !== getProduct()) {
    subtractPoint();
    incorrectGuess++;
} else if (input === '' || isNaN(input)) {
    console.log('Input value is empty or not a number', input);
} else {
    addPoint();
    correctGuesses++;
}
上述代码是您提供的JavaScript代码片段的一部分,其中包含了您提到的问题。在这段代码中,如果用户输入的是空字符串或NaN,会执行else-if分支中的代码块。
英文:
I am trying to evaluate when the user inputs nothing (an empty string) or anything besides a number (Not a number). After I console log the an input of empty string a NaN is returned. I am not sure why the else-if statement is never recognized if I test for both an empty string or NaN value. This also ultimately affects my average total score.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const equationTag = document.querySelector('div#equation');
const inputBtn = document.querySelector('input.submit-btn');
const incorrectTag = document.querySelector('p#incorrect');
const correctTag = document.querySelector('p#correct');
const counterTag = document.querySelector('div#counter');
const exitButton = document.querySelector('button.exit-btn');
const displayButton = document.querySelector('button.display-btn');
const resultModal = document.querySelector('section.stats-section');
const averageP = document.querySelector('p.avg');
const guessP = document.querySelector('p.total-guesses');
let points = 0;
let correctGuesses = 0;
let incorrectGuess = 0;
let totalGuesses = 0;
/*
    Takes a min and max value as parameters, and
    returns a randomized integer
*/
function getRandomValue(min, max) {
    let r = Math.floor(Math.random() * (max - min + 1)) + min;
    return r;
}
// Displays multiplcation equation on the user interface
function displayEquation() {
    equationTag.textContent = `${integerOne} x ${integerTwo}=`;
}
// Returns the product of the two integers
function getProduct() {
    return integerOne * integerTwo;
}
/* 
    Event listener grabs user input on click
    and clears user input afterwards
 */
inputBtn.addEventListener('click', () => {
    const inputTag = document.querySelector('#num');
    const answer = parseFloat(inputTag.value);
    evaluateAnswer(answer);
    inputTag.value = "";
    inputTag.focus();
})
/* 
    Event listener grabs user input on enter key 
    and clears user input afterwards
*/
document.addEventListener("keydown", (event) => {
    if (event.key === "Enter") {
        const inputTag = document.querySelector('#num');
        const answer = parseFloat(inputTag.value);
        evaluateAnswer(answer);
        inputTag.value = "";
        inputTag.focus();
    }
})
exitButton.addEventListener('click', () => {
    setDisplayNone(resultModal);
})
displayButton.addEventListener('click', () => {
    setDisplayBlock(resultModal);
})
/*
    Takes a integer user input as an argument
    and compares whether the answer is correct or not.
*/
function evaluateAnswer(input) {
    console.log('Input value on eval ', input); // double checking value
    if (input !== getProduct()) {
        subtractPoint();
        incorrectGuess++;
    } else if (input === ' ' || isNaN()) { // I am not sure why it's never evaluated
        console.log('Input value is empty or not a number ', input);
    } else {
        addPoint();
        correctGuesses++;
    }
    totalGuesses++;
    restartGame();
    guessP.textContent = "Incorrect Guesses= " + incorrectGuess;
    let average = (correctGuesses / totalGuesses);
    let precisionAvg = roundToPrecision(average, 2);
    averageP.textContent = `${(precisionAvg * 100).toFixed(2)}%`;
    // console.log('Total guesses: ', totalGuesses);
    // console.log('Incorrect ', incorrectGuess);
    // console.log("Average: ", average)
}
/*
    Evaluates if the points are less 
    than zero then restart points to 0
    else minus a point.
*/
function subtractPoint() {
    if (points <= 0) {
        points = 0;
    } else {
        points -= 1;
    }
    setDisplayBlock(incorrectTag);
    setDisplayNone(correctTag);
    incorrectTag.textContent = ('Incorrect: ' + integerOne + ' x ' + integerTwo + ' = ' + getProduct());
    setPoint();
}
// Sets new updated point
function setPoint() {
    counterTag.textContent = points;
}
// Adds a point and updates earned points
function addPoint() {
    points += 1;
    correctTag.textContent = ('Correct!');
    setDisplayBlock(correctTag);
    setDisplayNone(incorrectTag);
    setPoint();
}
/*
    Resets game and gets two new random integers
    and calls the displayEquation function.
*/
function restartGame() {
    integerOne = getRandomValue(0, 12);
    integerTwo = getRandomValue(0, 12);
    displayEquation();
}
// sets css display block and opacity 1 on element
function setDisplayBlock(displayResult) {
    displayResult.style.display = 'block';
    displayResult.style.opacity = 1;
}
// sets css display none and opacity 0 on element
function setDisplayNone(displayResult) {
    displayResult.style.display = 'none';
    displayResult.style.opacity = 0;
}
/*
    Takes a value as a parameter, and integer as a parameter
    returns a rounded value with two decimal places at most
    https://stackoverflow.com/questions/11832914/how-to-round-to-at-most-2-decimal-places-if-necessary/11832950#11832950
*/
function roundToPrecision(value, decimals = 2) {
    const pow = Math.pow(10, decimals);
    return Math.round((value + Number.EPSILON) * pow) / pow;
}
// run game on load
let integerOne = getRandomValue(0, 12);
let integerTwo = getRandomValue(0, 12);
displayEquation(); 
<!-- language: lang-html -->
<body>
    <header>
        <h1 id="title">Multiply Me</h1>
    </header>
    <main>
        <div id="equation"></div>
        <div id="counter">0</div>
        <input type="number" id="num" value="" title="input">
        <input type="submit" class="submit-btn">
        <button type="submit" class="display-btn" value="Show Results">&#x2b;</button>
        <div id="response">
            <p id="correct"></p>
            <p id="incorrect"></p>
        </div>
        <section class="stats-section">
            <h3 class="h3-title">Overall Results:</h3>
            <button class="exit-btn">x</button>
            <article class="article-stats">
                <p class="total-guesses"></p>
                <p class="avg"></p>
            </article>
        </section>
    </main>
</body>
<!-- end snippet -->
答案1
得分: 2
Several issues apart from not using isNaN correctly
You cannot see if a value is a single space after parseFloating it.
I would suggest
const answer = inputTag.value; 
evaluateAnswer(answer);
where you have this. Note the order and that I test the positive outcome before the negative
function evaluateAnswer(input) { 
  input = input.trim(); 
  if (input === "" || isNaN(input)) {   
    console.log('Input value is empty or not a number ', input);
    return; /* Any need to continue? */
  } else if (input === getProduct()) {
    addPoint();
    correctGuesses++;
  } else  {
    subtractPoint();
    incorrectGuess++;
  }
英文:
Several issues apart from not using isNaN correctly
You cannot see if a value is a single space after parseFloating it.
I would suggest
const answer = inputTag.value; 
evaluateAnswer(answer);
where you have this. Note the order and that I test the positive outcome before the negative
function evaluateAnswer(input) { 
  input = input.trim(); 
  if (input === "" || isNaN(input)) {   
    console.log('Input value is empty or not a number ', input);
    return; /* Any need to continue? */
  } else if (input === getProduct()) {
    addPoint();
    correctGuesses++;
  } else  {
    subtractPoint();
    incorrectGuess++;
  }
答案2
得分: 1
你首先必须检查输入值是否不为空,因此你必须修改条件的顺序。最好在没有输入值时添加return以退出函数,以避免生成新的问题。
function evaluateAnswer(input) {
    console.log('在评估中的输入值', input); // 再次检查值
    if (input === '' || isNaN(input)) { // 我不确定为什么它从未被评估
        console.log('输入值为空或不是数字', input);
        return;
    } else if (input !== getProduct()) {
        subtractPoint();
        incorrectGuess++;
    } else {
        addPoint();
        correctGuesses++;
    }
    totalGuesses++;
    restartGame();
    guessP.textContent = "不正确的猜测= " + incorrectGuess;
    let average = (correctGuesses / totalGuesses);
    let precisionAvg = roundToPrecision(average, 2);
    averageP.textContent = `${(precisionAvg * 100).toFixed(2)}%`;
}
英文:
You must first check that the input value is not empty, so you must modify the order of the condition. And it is better to add a return to exit the function when no value is entered so that a new question is not generated.
function evaluateAnswer(input) {
    console.log('Input value on eval ', input); // double checking value
    if (input === '' || isNaN(input)) { // I am not sure why it's never evaluated
        console.log('Input value is empty or not a number ', input);
        return;
    } else if (input !== getProduct()) {
        subtractPoint();
        incorrectGuess++;
    } else{
        addPoint();
        correctGuesses++;
    }
    totalGuesses++;
    restartGame();
    guessP.textContent = "Incorrect Guesses= " + incorrectGuess;
    let average = (correctGuesses / totalGuesses);
    let precisionAvg = roundToPrecision(average, 2);
    averageP.textContent = `${(precisionAvg * 100).toFixed(2)}%`;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论