Showing posts with label game. Show all posts
Showing posts with label game. Show all posts

Tuesday, 6 June 2023

3 Fun JavaScript Programs to Level Up Your Coding Skills || JS Projects

 

Sure! Here's an article featuring 3 JavaScript programs with code examples:

Title: 3 Fun JavaScript Programs to Level Up Your Coding Skills

Introduction:

JavaScript is a versatile programming language that can be used to build interactive web applications. If you're looking to sharpen your coding skills and have some fun along the way, here are 3 JavaScript programs that you can try out. Each program includes code examples to help you understand the implementation. Let's dive in!

  1. Number Guessing Game: The number guessing game is a classic program that challenges the user to guess a random number within a specified range. Here's an example implementation in JavaScript:

    
     let randomNumber = Math.floor(Math.random() * 100) + 1;
    let guessedNumber = prompt("Guess a number between 1 and 100:");

       if (guessedNumber == randomNumber) {
      console.log("Congratulations! You guessed the correct number.");
    } else {
      console.log("Oops! Better luck next time. The number was: "
       + randomNumber);
    }



  1. TODO List: Create a simple TODO list application that allows users to add and remove tasks. Here's an example implementation: 

             
                let todoList = [];
                   function addTask(task) {
                  todoList.push(task);
                }
                function removeTask(task) {
                  let index = todoList.indexOf(task);
                  if (index > -1) {
                    todoList.splice(index, 1);
                  }
                }

                addTask("Buy groceries");
                addTask("Finish homework");
                removeTask("Buy groceries");

                console.log(todoList);
                 // Output: ["Finish homework"]


  1. Palindrome Checker: A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. Check if a given word is a palindrome using JavaScript:

       
       function isPalindrome(word) {
         let reversedWord = word.split("").reverse().join("");
        return word === reversedWord;
      }

      console.log(isPalindrome("racecar")); // Output: true
      console.log(isPalindrome("hello")); // Output: false


Thanks for Visting Here ,.

Saturday, 3 June 2023

CodeQuest: The JavaScript Journey || A JavaScript Game

 


                           Create a Simple Game using javascript
 <!DOCTYPE html>
<html>
  <head>
    <title>Simple JavaScript Game</title>
    <style>
      #game-container {
        width: 400px;
        height: 400px;
        border: 1px solid black;
        position: relative;
      }
      #player {
        width: 50px;
        height: 50px;
        background-color: blue;
        position: absolute;
      }
    </style>
  </head>
  <body>
    <div id="game-container">
      <div id="player"></div>
    </div>
    <script >
            
// Get the player and game container elements
const player = document.getElementById("player");
const gameContainer = document.getElementById("game-container");

// Set the initial position of the player
let playerX = 0;
let playerY = 0;

// Function to handle keyboard arrow key events
function handleArrowKey(event) {
  const key = event.key;
  if (key === "ArrowUp" && playerY > 0) {
    playerY -= 10;
  } else if (key === "ArrowDown" && playerY < gameContainer.offsetHeight -
        player.offsetHeight) {
    playerY += 10;
  } else if (key === "ArrowLeft" && playerX > 0) {
    playerX -= 10;
  } else if (key === "ArrowRight" && playerX < gameContainer.offsetWidth -
        player.offsetWidth) {
    playerX += 10;
  }

  // Update the player's position
  player.style.left = playerX + "px";
  player.style.top = playerY + "px";
}

// Add event listener for arrow key events
document.addEventListener("keydown", handleArrowKey);

</script>
  </body>
</html>

Monday, 29 May 2023

Guess a number between 1 and 100 Game using HTML & JAVASCRIPT By getcodr





 <!DOCTYPE html>
<html>
<head>
  <title>Number Guessing Game</title>
</head>
<body>
  <h1>Number Guessing Game</h1>
  <p>Guess a number between 1 and 100:</p>
  <input type="text" id="guessInput">
  <button onclick="checkGuess()">Submit</button>
  <p id="message"></p>

  <script>
    // Generate a random number between 1 and 100
    var randomNumber = Math.floor(Math.random() * 100) + 1;
    var message = document.getElementById("message");

    function checkGuess() {
      var guessInput = document.getElementById("guessInput");
      var guess = parseInt(guessInput.value);

      if (isNaN(guess)) {
        message.textContent = "Invalid guess. Please enter a number.";
      } else if (guess < 1 || guess > 100) {
        message.textContent = "Please enter a number between 1 and 100.";
      } else {
        if (guess === randomNumber) {
          message.textContent = "Congratulations! You guessed the correct number.";
        } else if (guess < randomNumber) {
          message.textContent = "Too low. Try again.";
        } else {
          message.textContent = "Too high. Try again.";
        }
      }

      guessInput.value = ""; // Clear the input field
      guessInput.focus(); // Set focus back to the input field
    }
  </script>
</body>
</html>

                                  My YouTube Channel
                                        getcodr      


Rock, Paper, Scissors Game using HTML & JAVASCRIPT by getcodr





 <!DOCTYPE html>
<html>
<head>
  <title>Rock, Paper, Scissors Game</title>
</head>
<body>
  <h1>Rock, Paper, Scissors Game</h1>
  <p>Make your choice:</p>
  <button onclick="playGame('rock')">Rock</button>
  <button onclick="playGame('paper')">Paper</button>
  <button onclick="playGame('scissors')">Scissors</button>
  <p id="result"></p>

  <script>
    function playGame(userChoice) {
      var choices = ["rock", "paper", "scissors"];
      var computerChoice = choices[Math.floor(Math.random() * choices.length)];
      var result = document.getElementById("result");

      result.textContent = "Your choice: " + userChoice
                           + " | Computer's choice: " + computerChoice;

      if (userChoice === computerChoice) {
        result.textContent += " | It's a tie!";
      } else if (
        (userChoice === "rock" && computerChoice === "scissors") ||
        (userChoice === "paper" && computerChoice === "rock") ||
        (userChoice === "scissors" && computerChoice === "paper")
      ) {
        result.textContent += " | You win!";
      } else {
        result.textContent += " | Computer wins!";
      }
    }
  </script>
</body>
</html>


                                MY YOUTUBE CHANNEL
                                     getcodr


This is me