For LEarning purposes

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.6 kB
1
Indexable
Never
'use strict';

// console.log(document.querySelector('.message').textContent);

// document.querySelector('.message').textContent = 'Correct Number 🥃';

// console.log(document.querySelector('.message').textContent);

// document.querySelector('.number').textContent = 13;
// document.querySelector('.score').textContent = 10;

// console.log(document.querySelector('.guess').value);
// document.querySelector('.guess').value = 23;
// console.log(document.querySelector('.guess').value);


  let secretNumber = Math.trunc(Math.random() * 20) + 1;
  let score = 20;

  document.querySelector('.check').addEventListener('click', function () {
  const guess = Number(document.querySelector('.guess').value);
  console.log(guess, typeof guess);

  document.querySelector('.again').addEventListener('click', function(){
          document.querySelector('.message').textContent = 'Start guessing...';
          document.querySelector('.number').textContent = '?';
          document.querySelector('.guess').value = '';
          document.querySelector('body').style.backgroundColor = "#222";
          document.querySelector('.number').style.width = '15rem';
          score = 20;
          document.querySelector('.score').textContent =  score;
          secretNumber = Math.trunc(Math.random() * 20) + 1;

  })

  // If there is no input
  if (!guess) 
  {
    document.querySelector('.message').textContent = '⚠ No number!';
  }
  // If player wins
  else if (guess === secretNumber) {
    document.querySelector('.message').textContent = 'Correct Number! ';
    document.querySelector('.number').textContent = secretNumber;
    document.querySelector('body').style.backgroundColor = '#60b347';
    document.querySelector('.number').style.width = '30rem';
  
  } 

      //When guess is too high
      if (guess > secretNumber) 
      {
          if(score > 1){
          document.querySelector('.message').textContent = 'Too high!';
          score--;
          document.querySelector('.score').textContent =  score;
          }
          else{
            document.querySelector('.score').textContent =  0;
            document.querySelector('.message').textContent= 'YOU LOST THE GAME'}
      }
      //When Guess is too low 
      else if (guess < secretNumber) 
      {
        if(score > 1){
        document.querySelector('.message').textContent = 'Too low!';
        score--;
        document.querySelector('.score').textContent =  score;
        }
        else{
          document.querySelector('.score').textContent =  0;
          document.querySelector('.message').textContent= 'YOU LOST THE GAME'}
      }
  

  });