Challenge Day 4

Number Guessing Game

Day 4 of this coding challenge had a different vibe because I was asked to create a simple game: Number Guessing Game! This challenge taught me about random number generation, user interaction, and conditional logic.

Challenge Description

The program should be able to:

  1. Generate a random number between 1 and 10
  2. Ask the user to guess the number
  3. If wrong:
    • Give a clue: "Too small" or "Too big"
    • Repeat the guess
  4. If correct:
    • Show success message + number of attempts
Example Output

I'm thinking of a number between 1 and 10.
Take a guess!
Your guess: 4
Too small!
Your guess: 7
Too big!
Your guess: 6
Congratulations! You guessed the number correctly in 3 attempts.
          

Funny Debugging

At first I wrote it like this:

Initial Mistake

const randomNumber:
Math.floor(Math.random() * 10) + 1:
          

And of course it errored. I was confused for a moment. Turns out I used : when I should've used = for assignment. Classic JavaScript typo.

Initial Fixed Version

After fixing and rearranging the logic, here's my first working version:

Working Version

let randomNumber = Math.floor(Math.random() * 10) + 1;
let attempt = 0;
let repeat = true;

while(repeat) {
  const userInput = prompt("I'm thinking of a number between 1 and 10. Take a guess!");
  attempt++;

  const guess = parseInt(userInput);

  if (isNaN(guess)) {
    console.log("Eh? Please enter a number 😄");
  } else {
    if (guess < randomNumber) {
      console.log("Too small 😅");
    } else if (guess > randomNumber) {
      console.log("Too big 😅");
    } else {
      console.log(`🎉 Congratulations! You guessed the number correctly in ${attempt} attempts.`);
      repeat = false;
    }
  }
}
          

The code is simple and works immediately. But I felt it could still be improved.

Bonus Challenge

There were additional optional challenges:

  • Number input validation
  • Scoring system (comments based on number of attempts)
  • Hard mode: 1-50

Input Validation & Scoring

Number validation was already implemented using isNaN(). Just needed to add the scoring system logic:

Scoring System

if(attempt <= 3) {
  console.log("🔥 Pro move! You guessed it super fast.");
} else if (attempt <= 6) {
  console.log("👍 Not bad! But you can do better.");
} else {
  console.log("😅 Well, needs more practice. Keep trying!");
}
          

Hard Mode (1-50)

I used confirm() to ask if the user wants to play in hard mode:

Hard Mode Implementation

let hardMode = confirm("Want to play in hard mode? (Numbers 1–50)");
let rangeText = hardMode ? "1 and 50" : "1 and 10";
let randomNumber = Math.floor(Math.random() * (hardMode ? 50 : 10)) + 1;
          

Then I included rangeText in the prompt() to make it dynamic:

Dynamic Prompt

const userInput = prompt(`I'm thinking of a number between ${rangeText}. Take a guess!`);
          

Final Version: Fully Interactive

Complete Solution

let hardMode = confirm("Want to play in hard mode? (Numbers 1–50)");
let rangeText = hardMode ? "1 and 50" : "1 and 10";
let randomNumber = Math.floor(Math.random() * (hardMode ? 50 : 10)) + 1;
let attempt = 0;
let repeat = true;

while(repeat) {
  const userInput = prompt(`I'm thinking of a number between ${rangeText}. Take a guess!`);
  attempt++;

  const guess = parseInt(userInput);

  if (isNaN(guess)) {
    console.log("Eh? Please enter a number 😄");
  } else {
    if (guess < randomNumber) {
      console.log("Too small 😅");
    } else if (guess > randomNumber) {
      console.log("Too big 😅");
    } else {
      console.log(`🎉 Congratulations! You guessed the number correctly in ${attempt} attempts.`);

      if(attempt <= 3) {
        console.log("🔥 Pro move! You guessed it super fast.");
      } else if (attempt <= 6) {
        console.log("👍 Not bad! But you can do better.");
      } else {
        console.log("😅 Well, needs more practice. Keep trying!");
      }

      repeat = false;
    }
  }
}
          

💡 Reflection

What I learned from this challenge:

  • The importance of careful syntax writing (small typos can cause big errors)
  • if-else logic can be enhanced to be more interactive and dynamic
  • confirm() and prompt() make the user experience more engaging
  • Debugging can actually be fun when treated as learning material

And of course, I'm more excited to continue to Day 5 🚀

View Source Code
Your Logo