Skip to content

Conversation

thsh-odoo
Copy link

Completed the task of creating a small basic game of rock paper scissor

task1.js Outdated
@@ -0,0 +1,69 @@
const choices=['Rock','Paper','Scissor'];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give proper spacing while assignment:
const choices = ['Rock','Paper','Scissor'];
update it for all cases in the code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sir

task1.js Outdated
Comment on lines 3 to 69
function rock()
{
let computer_choice=Math.floor(Math.random() * choices.length);
console.log(choices[computer_choice])

if(choices[computer_choice]=='Rock')
{
// console.log("helllllllo")
document.getElementById("status").textContent="Draw";
}
else if (choices[computer_choice]=='Paper')
{
document.getElementById("status").textContent="Computer Wins";
computer_score+=1;
document.getElementById("computer_score").textContent=computer_score;
console.log(computer_score)
}
else if(choices[computer_choice]=='Scissor')
{
document.getElementById("status").textContent="You Win";
user_scores+=1;
document.getElementById("user_scores").textContent=user_scores;
console.log(user_scores)
}
}
function paper()
{
let computer_choice=Math.floor(Math.random() * choices.length);
console.log(choices[computer_choice])
if(choices[computer_choice]=='Paper')
{
document.getElementById("status").textContent="Draw";
}
else if (choices[computer_choice]=='Scissor')
{
document.getElementById("status").textContent="Computer Wins";
computer_score+=1;
document.getElementById("computer_score").textContent=computer_score;
}
else if(choices[computer_choice]=='Rock')
{
document.getElementById("status").textContent="You Win";
user_scores+=1;
document.getElementById("user_scores").textContent=user_scores;
}
}
function scissor()
{
let computer_choice=Math.floor(Math.random() * choices.length);
console.log(choices[computer_choice])
if(choices[computer_choice]=='Scissor')
{
document.getElementById("status").textContent="Draw";
}
else if (choices[computer_choice]=='Rock')
{
document.getElementById("status").textContent="Computer Wins";
computer_score+=1;
document.getElementById("computer_score").textContent=computer_score;
}
else if(choices[computer_choice]=='Paper')
{
document.getElementById("status").textContent="You Win";
user_scores+=1;
document.getElementById("user_scores").textContent=user_scores;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making separate function call for separate event is good.
But optimize the code:

  1. using ternary operator. as user can win in three cases only :
    you_win = ( (you === "rock") && (opponent === "scissor") ||
    (you === "paper") && (opponent === "rock") ||
    (you === "scissor") && (opponent === "rock") ) // returns true for your win cases only.
    .
    .
    so (you === opponent) ? "draw message" : you_win ? call a method for updating score and message for user : call method for updating score and message fro computer;

3.(note* for this case only.) use concept of querySelectorAll("button"), and foreach click call a method that update the state of your game.
4. declare it a const computer_choice=Math.floor(Math.random() * choices.length); and on every time your method calls while button click , you will get a random method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sir
I have optimize my code in today's RPS game using owl I will do it same in this code without owl

task1.js Outdated

if(choices[computer_choice]=='Rock')
{
// console.log("helllllllo")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discard the unwanted/ commeted out code while you push the code. make it a habit of checking the code you push.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants