mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-27 06:40:42 -05:00
added tictactoe.js
This commit is contained in:
parent
45d1442742
commit
1fcb68f4e4
5 changed files with 103 additions and 0 deletions
17
dom-exercise/index.html
Normal file
17
dom-exercise/index.html
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<button onclick="alert('Hello World')">Click Me</button>
|
||||||
|
<!-- the HTML file -->
|
||||||
|
<button id="btn">Click Me</button>
|
||||||
|
<script src="js/script.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
36
dom-exercise/js/script.js
Normal file
36
dom-exercise/js/script.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
const container = document.querySelector('.container');
|
||||||
|
|
||||||
|
const content = document.createElement('div');
|
||||||
|
|
||||||
|
content.classList.add('dynamic');
|
||||||
|
content.textContent = "This shit is added";
|
||||||
|
container.appendChild(content);
|
||||||
|
|
||||||
|
const paragraph = document.createElement('p');
|
||||||
|
paragraph.textContent = 'Hey, I\'m red';
|
||||||
|
paragraph.style.color = 'red';
|
||||||
|
container.appendChild(paragraph);
|
||||||
|
|
||||||
|
const headerThree = document.createElement('h3');
|
||||||
|
headerThree.textContent = "Hey, I'm blue h3!";
|
||||||
|
headerThree.style.color = 'blue';
|
||||||
|
container.appendChild(headerThree);
|
||||||
|
|
||||||
|
const newDiv = document.createElement('div');
|
||||||
|
newDiv.style.borderColor = 'black';
|
||||||
|
newDiv.style.backgroundColor = 'pink';
|
||||||
|
|
||||||
|
const innerHeader = document.createElement('h1');
|
||||||
|
innerHeader.textContent = "I'm in a div!";
|
||||||
|
const paragraphTwo = document.createElement('p');
|
||||||
|
paragraphTwo.textContent = "Me too!!!";
|
||||||
|
|
||||||
|
newDiv.appendChild(innerHeader);
|
||||||
|
newDiv.appendChild(paragraphTwo);
|
||||||
|
container.appendChild(newDiv);
|
||||||
|
|
||||||
|
// the JavaScript file
|
||||||
|
const btn = document.querySelector('#btn');
|
||||||
|
btn.addEventListener('click', () => {
|
||||||
|
alert("Hello World");
|
||||||
|
});
|
|
@ -7,5 +7,6 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script src="script.js"></script>
|
<script src="script.js"></script>
|
||||||
|
<script src="tictactoe.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
0
javascript/javascript.js
Normal file
0
javascript/javascript.js
Normal file
49
javascript/tictactoe.js
Normal file
49
javascript/tictactoe.js
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
// TODO
|
||||||
|
/*
|
||||||
|
Simple TTT game.
|
||||||
|
|
||||||
|
User chooses R,P,S and then the computers answer should be randomly generated
|
||||||
|
from those 3 options
|
||||||
|
|
||||||
|
method to compare users answer vs computers
|
||||||
|
rock > scissors && rock < paper
|
||||||
|
scissors > paper && scissors < rock
|
||||||
|
pape > rock && paper < scissors
|
||||||
|
repeat if user_ans == computer_ans
|
||||||
|
|
||||||
|
|
||||||
|
if either player wins log output
|
||||||
|
else if its a tie , it should prompt the user to pick a new answer
|
||||||
|
until a winner is picked
|
||||||
|
*/
|
||||||
|
|
||||||
|
let rpc = ["rock", "paper", "scissors"]
|
||||||
|
|
||||||
|
let usersChoice = () => {
|
||||||
|
let userChoice = prompt("Rock, Paper, or Scissors? ");
|
||||||
|
return userChoice.toLocaleLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
let getCPUChoice = () => {
|
||||||
|
// Generate a random number between 0 and 2
|
||||||
|
let choice = Math.floor(Math.random() * 3);
|
||||||
|
|
||||||
|
return rpc[choice];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getResults(playerSelection, cpuSelection) {
|
||||||
|
if (playerSelection === cpuSelection) {
|
||||||
|
return "tie";
|
||||||
|
} else if (playerSelection == "rock" && cpuSelection == "scissors" ||
|
||||||
|
playerSelection == "paper" && cpuSelection == "rock" ||
|
||||||
|
playerSelection == "scissors" && cpuSelection == "paper") {
|
||||||
|
return `You Win! ${playerSelection} beats ${cpuSelection}`;
|
||||||
|
} else {
|
||||||
|
return `You Lose! ${cpuSelection} beats ${playerSelection}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uc = usersChoice();
|
||||||
|
cpc = getCPUChoice();
|
||||||
|
console.log(uc, cpc);
|
||||||
|
console.log(getResults(uc, cpc));
|
Loading…
Reference in a new issue