Skip to content

JavaScript Course: More Testing - Edit guessingGame function example #29916

@mozartdd

Description

@mozartdd

Checks

Describe your suggestion

In the Pure Functions lesson, the guessingGame example is used to demonstrate separating logic from side effects. The current version uses prompt once to get the user’s guess, but it does not handle the case where the user enters a number outside the valid range (1–100) or cancels/enters nothing.

It might be helpful for learners if the lesson showed a simple while loop to re-prompt the user until they enter a valid number between 1 and 100.
This way, the evaluateGuess function stays pure and focused on the comparison logic, while input validation happens in the main game loop — keeping responsibilities clear and testable.

function guessingGame() {
const magicNumber = 22;
let guess;

do {
guess = Number(prompt('Guess a number between 1 and 100!'));
} while (isNaN(guess) || guess < 1 || guess > 100);

const message = evaluateGuess(magicNumber, guess);
alert(message);
}

Path

Node / JS

Lesson Url

https://www.theodinproject.com/lessons/node-path-javascript-more-testing

(Optional) Discord Name

No response

(Optional) Additional Comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions