<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guess the Number Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#result {
font-size: 1.5em;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Mchezo wa kufikiria namba</h1>
<p>Fikiria namba kati ya 1 hadi 10:</p>
<input type="number" id="userGuess">
<button onclick="checkGuess()">Submit Guess</button>
<div id="result"></div>
<script>
// Generate a random number between 1 and 10
const secretNumber = Math.floor(Math.random() * 10) + 1;
function checkGuess() {
// Get user's guess from the input field
const userGuess = document.getElementById('userGuess').value;
// Check if the input is a valid number
if (!isNaN(userGuess) && userGuess !== '') {
// Convert the user's guess to a number
const userNumber = parseInt(userGuess, 10);
// Check if the guess is correct
if (userNumber === secretNumber) {
document.getElementById('result').innerHTML = 'Hongera umefikiria namba sawa';
} else {
//document.getElementById('result').innerHTML = 'Jaribu tena, umechaguwa namba '+;
if (userGuess > secretNumber){document.getElementById('result').innerHTML = 'Jaribu tena, umechaguwa namba,
Umechaguwa namba kubwa'}else{
document.getElementById('result').innerHTML = 'Jaribu tena, umechaguwa namba ndogo.'
}
}
} else {
// Handle the case where the input is not a valid number
document.getElementById('result').innerHTML = 'Weka namba kati ya 1 hadi 10.';
}
}
</script>
</body>
</html>
Game 3: Mchezo wa kufikiria namba kwa kutumia javascript
Hii ni game ambayo utafikiria namba, ambayo inatakiwa iwe sawa na namba ambayo game imeiweka.