A game of Rock Paper Scissors written in JavaScript ✋

  Рет қаралды 58,177

Bro Code

Bro Code

Күн бұрын

Пікірлер: 53
@BroCodez
@BroCodez 2 жыл бұрын
const playerText = document.querySelector("#playerText"); const computerText = document.querySelector("#computerText"); const resultText = document.querySelector("#resultText"); const choiceBtns = document.querySelectorAll(".choiceBtn"); let player; let computer; let result; choiceBtns.forEach(button => button.addEventListener("click", () => { player = button.textContent; computerTurn(); playerText.textContent = `Player: ${player}`; computerText.textContent = `Computer: ${computer}`; resultText.textContent = checkWinner(); })); function computerTurn(){ const randNum = Math.floor(Math.random() * 3) + 1; switch(randNum){ case 1: computer = "ROCK"; break; case 2: computer = "PAPER"; break; case 3: computer = "SCISSORS"; break; } } function checkWinner(){ if(player == computer){ return "Draw!"; } else if(computer == "ROCK"){ return (player == "PAPER") ? "You Win!" : "You Lose!" } else if(computer == "PAPER"){ return (player == "SCISSORS") ? "You Win!" : "You Lose!" } else if(computer == "SCISSORS"){ return (player == "ROCK") ? "You Win!" : "You Lose!" } } Document Player: Computer: Result: ROCK PAPER SCISSORS .choiceBtn{ line-height: 30px; width: 150px; } #gameDiv{ font-family: 'Brush Script MT', cursive; border: 3px solid; border-radius: 25px; padding: 10px; background-color: lightgrey; text-align: center; } #playerText{ color: blue; } #computerText{ color: red; }
@biorix9704
@biorix9704 2 жыл бұрын
He just copy pasted entire code in a comment💀💀
@Trexzilla
@Trexzilla Жыл бұрын
@@biorix9704 but mine isn't working
@percussion1999
@percussion1999 3 ай бұрын
Nice sor
@ninadbhide1282
@ninadbhide1282 2 жыл бұрын
Thank you Bro, I am learning JS using your tutorials
@MrTitanfr
@MrTitanfr 2 жыл бұрын
hey bro...love your work man...i` m learning so much from you...thanks for the time and effort plz keep up the good work...bless you
@Customkettlesdublin
@Customkettlesdublin 2 жыл бұрын
this video earned my sub, making everything so simple to follow . wasnt working for like 5 mins thought i wasted time but just my script tag was inside my div :))
@mayowafalomo1399
@mayowafalomo1399 2 жыл бұрын
You didn't disappoint me when i needed this video, Thank you soo much.
@joshuwa3953
@joshuwa3953 2 жыл бұрын
How can we implement a score tracker for this ?
@iitzrohan
@iitzrohan Жыл бұрын
Here's How i did it. const playerScore = document.getElementById("playerScore"); const computerScore = document.getElementById("computerScore"); let result; let countplayer = 0;; let countComputer = 0; choiceBtns.forEach(button => button.addEventListener("click", () => { computerTurn(); player = button.textContent; playerText.textContent = `Player: ${player}`; computerText.textContent = `Computer: ${computer}`; result = checkWinner(); resultText.textContent = result; if (result == "You Win!") { countplayer += 1; playerScore.textContent = countplayer; } if (result == "You Lose!") { countComputer += 1; computerScore.textContent = countComputer; } }));
@diegoserrano8302
@diegoserrano8302 Жыл бұрын
thank you for the video i have a deadline for a project coming and this helped me a lot!!
@dianadoroi154
@dianadoroi154 2 жыл бұрын
Thank you so much for your work!
@lucabinder9829
@lucabinder9829 2 жыл бұрын
I have already watched 87 videos for JavaScript. All from you.
@kmdoll
@kmdoll 2 жыл бұрын
for the youtube algorithmmmm
@cashvillekid98
@cashvillekid98 8 ай бұрын
The check winner function doesn't check all possible conditions...like what if computer chooses paper and the player chooses rock?
@kwazeo
@kwazeo 2 жыл бұрын
You are the best! :D
@josephquezada6138
@josephquezada6138 Жыл бұрын
hey you never used the result variable, also why did you put *3 + 1?
@agent-33
@agent-33 Жыл бұрын
Good eye. The result variable is unused and must be removed from the code. The Math.random() * 3 + 1 within Math.floor() will produce number from 1 to 3. You might be confused what is the point of + 1 there, to understand this, we must understand what the code Math.random() alone, can produce. Math.random() returns a float from 0.00000 to 0.999999, in other words, it gives 0 to less than 1. If we use Math.random() * 3, this will produce numbers from 0.000000 to 2.999999. If we put it inside Math.floor(), it will remove the decimals so the result will range from 0 to 2. Actually, this should be enough to produce what we want, 3 different numbers: 0, 1, 2. But the coder want a number range from 1~3 (1, 2, 3) so he put + 1 to offset the result of random number by additional 1.
@Maha-sj2qx
@Maha-sj2qx Жыл бұрын
how come the results variable is never used but it is declared?
@MacN_
@MacN_ Жыл бұрын
Hiya, thank you for your videos! I completed this program, however my machine didn't like a section of code. Here is the error message: uncaught typeError: "choiceBtns.forEach" is not a function. line 9. running through VS code, live server. Other than that the program looks how it should. Thank you, MacN (anonymous)
@MacN_
@MacN_ Жыл бұрын
any suggestions to remedy are appreciated.
@TacNral
@TacNral Жыл бұрын
@@MacN_ Hi, I have experienced the same problem, if you haven't found and answer yet it might be because it needs to be querySelectorAll instead of querySelector
@yy.u.i
@yy.u.i Жыл бұрын
​​@@TacNralThank you. I had the same problem, but after adding the 'All' it was resolved
@nemeandragon9062
@nemeandragon9062 Жыл бұрын
I tried following along and doing it myself but I couldn't get the live server to work as it should... After copying your code completely, the web page still doesn't react whenever I click on the buttons. Wondering if I have something incorrectly configured inside of VS code?
@mst3ksanta
@mst3ksanta 2 жыл бұрын
I haven't played htis version of pokemon for a long time
@малосольные-окурки
@малосольные-окурки 7 ай бұрын
great video! lesgo!
@sherezali9691
@sherezali9691 Жыл бұрын
thanksssssssssssssssssssssssssss brother🥰🥰🥰
@shaneforde5613
@shaneforde5613 2 жыл бұрын
Great for beginners
@sayyedshoeib7107
@sayyedshoeib7107 2 жыл бұрын
how to find ho is winner after the player or computer point is 5,10 final result
@Islamicway6855
@Islamicway6855 2 жыл бұрын
Which software you use for this?
@tintedclown4876
@tintedclown4876 2 жыл бұрын
Visual studio code
@edisonviolante
@edisonviolante 11 ай бұрын
Could someone helpme out about this code? I would like to improve it with an total score in the corner and switching the "Player" name for the real name when acessing the page (Idk if by prompt would be a good choice)
@edisonviolante
@edisonviolante 11 ай бұрын
and maybe a win-streak counter to
@amniar8158
@amniar8158 2 жыл бұрын
thanks so much man
@marxLz
@marxLz 2 жыл бұрын
🙏🙏🙏 For the youtube algorithm.
@sleepyfeokx
@sleepyfeokx 2 жыл бұрын
function computerTurn() { const randNum = Math.floor(Math.random() * 3) + 1;
@Trexzilla
@Trexzilla Жыл бұрын
mine isn`t working
@jailenegonzalez4350
@jailenegonzalez4350 9 ай бұрын
mine either !
@livegaming3051
@livegaming3051 2 жыл бұрын
I prefer to code rock paper scissors in f*ing unity than this 💀
@Cloud_with_lois
@Cloud_with_lois Жыл бұрын
nobody cares
@ShadowedSpectre
@ShadowedSpectre Жыл бұрын
shut up. You just dont know how to code in REAL programming languages💀💀💀
@sayyedshoeib7107
@sayyedshoeib7107 2 жыл бұрын
awosome
@sarthakladfacts945
@sarthakladfacts945 Жыл бұрын
❤❤
@pranayshelke8666
@pranayshelke8666 2 жыл бұрын
Thanks budyy
@thehoeynator
@thehoeynator 2 жыл бұрын
I followed it step for step and it didn't work
@secretsdowntherain5894
@secretsdowntherain5894 2 жыл бұрын
same here
@rolando8966
@rolando8966 Жыл бұрын
It looks like he added an “s” to “Btn” in JavaScript…. But not in HTML… that might be why
@rolando8966
@rolando8966 Жыл бұрын
Or maybe u pressed you put a “single quote “ vs a “backTick” it’s the button above “tab” key
@rolando8966
@rolando8966 Жыл бұрын
At 6:28 min mark
@percussion1999
@percussion1999 3 ай бұрын
Hii😊
@shubhangichavan6925
@shubhangichavan6925 2 жыл бұрын
for yt
@Gunz1234
@Gunz1234 Жыл бұрын
fix for the Uncaught TypeError: choiceBtn.forEach is not a function const choiceBtn = document.getElementsByClassName("//urID"); Array.from(choiceBtn).forEach(button => button.addEventListener("click", () => { player = button.textContent; computerTurn(); playerText.textContent = `Player: ${player}`; computerText.textContent = `Computer: ${computer}`; resultText.textContent = checkWinner(); })); basically add Array.from().forEach put values and it will work
@Mrmeepdev
@Mrmeepdev Жыл бұрын
The value in the video was choiceBtns = document.querySelectorAll('.choiceBtn') and not ChoiceBtn.document.querySelectorAll('.choiceBtn') You probably had a TypeError when writing the forEach function
Build JavaScript ROCK PAPER SCISSORS in 18 minutes! 👊
18:54
A game of Snake written in JavaScript 🐍
28:50
Bro Code
Рет қаралды 83 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Learn Javascript  Rock, Paper, Scissors
26:28
CodeWithRana
Рет қаралды 9 М.
THE ODIN PROJECT: FOUNDATIONS - ROCK PAPER SCISSORS | PROJECT SOLUTION
26:06
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 281 М.
A game of Pong written in JavaScript 🏓
24:48
Bro Code
Рет қаралды 77 М.
How To Code Rock Paper Scissors In JavaScript
21:12
Web Dev Simplified
Рет қаралды 88 М.
Create Rock Paper Scissors in Java in 10 Minutes
10:24
Coding with John
Рет қаралды 122 М.
Slide Puzzle Game in JavaScript HTML CSS
25:38
Kenny Yip Coding
Рет қаралды 41 М.
Стрелочные функции в JavaScript
27:42
Александр Дудукало
Рет қаралды 7 М.
ROCK PAPER SCISSORS game in Python 🗿
9:29
Bro Code
Рет қаралды 142 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН