Make a Javascript code that is like the tv show "press your luck"

3 answers

Here is a simple Javascript code that simulates a game similar to the TV show "Press Your Luck":

```javascript
let playerScore = 0;
let spinsRemaining = 3;

function spin() {
let spinResult = Math.floor(Math.random() * 10) + 1;
console.log(`You spun: ${spinResult}`);

if (spinResult === 1) {
console.log("Whammy! You lose all your points and a turn.");
playerScore = 0;
spinsRemaining--;
} else {
playerScore += spinResult;
console.log(`Your score is now: ${playerScore}`);
}

checkWin();
}

function checkWin() {
if (playerScore >= 100) {
console.log("Congratulations! You won the game!");
} else if (spinsRemaining === 0) {
console.log("You ran out of spins. Game over.");
} else {
console.log(`Spins remaining: ${spinsRemaining}`);
console.log("Press 's' to spin again or 'q' to quit.");
}
}

function playGame() {
console.log("Welcome to Press Your Luck!");
console.log("Spin the wheel to earn points. Get to 100 points to win.");

while (spinsRemaining > 0 && playerScore < 100) {
let userInput = prompt("Press 's' to spin or 'q' to quit.");

if (userInput === 's') {
spin();
} else if (userInput === 'q') {
console.log("Thanks for playing!");
break;
} else {
console.log("Invalid input. Please try again.");
}
}
}

playGame();
```

This code creates a simple game where the player can spin a virtual wheel to earn points. The objective is to reach 100 points without landing on a "whammy", which resets the player's score to 0. The player has 3 spins to achieve this goal. The game will prompt the player to either continue spinning or quit after each spin. The game ends when the player reaches 100 points or runs out of spins.
Its not functioning right give me a better one
Sure, here is an updated version of the game:

```javascript
let playerScore = 0;
let spinsRemaining = 3;
let whammy = false;

function spin() {
let spinResult = Math.floor(Math.random() * 10) + 1;
console.log(`You spun: ${spinResult}`);

if (spinResult === 1) {
console.log("Whammy! You lose all your points and a turn.");
playerScore = 0;
whammy = true;
} else {
playerScore += spinResult;
console.log(`Your score is now: ${playerScore}`);
}

checkWin();
}

function checkWin() {
if (playerScore >= 100) {
console.log("Congratulations! You won the game!");
} else if (spinsRemaining === 0) {
console.log("You ran out of spins. Game over.");
} else {
console.log(`Spins remaining: ${spinsRemaining}`);
if (!whammy) {
console.log("Press 's' to spin again or 'q' to quit.");
} else {
console.log("You hit a Whammy! Press 's' to spin again or 'q' to quit.");
}
}
}

function playGame() {
console.log("Welcome to Press Your Luck!");
console.log("Spin the wheel to earn points. Get to 100 points to win.");

while (spinsRemaining > 0 && playerScore < 100) {
let userInput = prompt("Press 's' to spin or 'q' to quit.");

if (userInput === 's') {
spin();
if (!whammy) {
spinsRemaining--;
}

whammy = false;
} else if (userInput === 'q') {
console.log("Thanks for playing!");
break;
} else {
console.log("Invalid input. Please try again.");
}
}
}

playGame();
```

This code should now properly handle the game logic for "Press Your Luck". It keeps track of the player's score, remaining spins, and whether they hit a "whammy". The player can keep spinning until they reach 100 points or run out of spins. The prompt will correctly display the message based on the player's last spin result.