Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
var correctCount = 0;
var guessCorrectlyToWin = 2;

var main = function (input) {
var myOutputValue = 'hello world';
// Generate the random secret word
var secretWord = computerChoice();

// Check if user has guessed correctly
var correctOrWrongGuess = didUserGuessedCorrectly(input, secretWord);

// Check if user wins
var userWin = didUserWin(correctCount);

var myOutputValue = `You guessed ${input}. The secret word is ${secretWord}. <br ><br >${correctOrWrongGuess}.<br ><br >${userWin}.`;

return myOutputValue;
};

// Function to generate random secret word
var computerChoice = function () {
// Generate random number
var randomNum = Math.floor(Math.random() * 3);

// Assign the random number generated to each secret word
if (randomNum == 0) {
var randomSecretWord = "banana";
}
if (randomNum == 1) {
var randomSecretWord = "chisel";
}
if (randomNum == 2) {
var randomSecretWord = "faucet";
}
return randomSecretWord;
};

// Function to check if user's input is equals to the random secret word generated by the computer
var didUserGuessedCorrectly = function (userInput, secretWord) {
var results = `You are wrong`;
if (userInput == secretWord) {
correctCount = correctCount + 1;
var results = `You are correct`;
}
return results;
};

// Function to check the number of times that user has guessed correctly
var didUserWin = function (input) {
var winResults = `Try Again!`;

// If the number of times user guessed correctly is equals to the number of times required to win, the program will output user wins
if (input == guessCorrectlyToWin) {
correctCount = 0;
winResults = `You have guessed 2 times correctly.<br ><br >You Won! Submit an input to play again!`;
return winResults;
}

// If the number of times user guessed correctly is lesser than the number of times required to win, the program will output the remaining number of times required
if (input < guessCorrectlyToWin) {
var correctCountRequired = guessCorrectlyToWin - input;
winResults = `You have guessed ${input} times correctly.<br ><br >You need to guess ${correctCountRequired} more correct guess to win`;
}

return winResults;
};