Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Austin Covey Submission #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 25 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta charset="utf-8" />
<title>Tic Tac Toe</title>
<!-- link to CSS code -->
<link rel="stylesheet" href="tictactoe.css">
<link rel="stylesheet" href="tictactoe.css" />
<!-- link to JavaScript code -->
<script src="scripts.js"></script>
</head>
Expand All @@ -14,14 +14,33 @@
<h1>Welcome to Tic Tac Toe</h1>
<p>Get ready to play!</p>
<!-- uses the built-in onclick attribute to call the resetBoard function -->
<button onclick="resetBoard()" type="button" class="btn btn-primary btn-lg" name="button">Restart</button>
<button
onclick="resetBoard()"
type="button"
class="btn btn-primary btn-lg"
name="button"
>
Restart
</button>
</div>
<table>
<tr>
<!-- the onclick calls a function called "handleClick" and passes itself to it -->
<td id='top-left' onclick="handleClick(this)" ></td>
<td id='top-middle'></td>
<td></td>
<td id="top-left" onclick="handleClick(this)"></td>
<td id="top-middle" onclick="handleClick(this)"></td>
<td id="top-right" onclick="handleClick(this)"></td>
</tr>
<tr>
<!-- the onclick calls a function called "handleClick" and passes itself to it -->
<td id="middle-left" onclick="handleClick(this)"></td>
<td id="middle-middle" onclick="handleClick(this)"></td>
<td id="middle-right" onclick="handleClick(this)"></td>
</tr>
<tr>
<!-- the onclick calls a function called "handleClick" and passes itself to it -->
<td id="bottom-left" onclick="handleClick(this)"></td>
<td id="bottom-middle" onclick="handleClick(this)"></td>
<td id="bottom-right" onclick="handleClick(this)"></td>
</tr>
<!-- @TODO create new rows and cells for the rest of your TTT board. -->
<!-- @TODO give each new cell an id & an event listener to call the "handleClick" function -->
Expand Down
98 changes: 37 additions & 61 deletions scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,79 @@

// 1. Read the code below one block at a time.
// 2. Look for the @TODOs, and figure out how to fix them.
// next to each @TODO you will find tasks that need to be finished
// next to each @TODO you will find tasks that need to be finished

// The variable will change from X to O based on what player turn it is. We need to hold this so we can place an X or O on the board when they're clicked.
let currentMarker = 'X'
let currentMarker = "X";




// this "handleClick" function is called when a box is clicked. Here, "element" will hold the same value as "this" does in the HTML.
// this "handleClick" function is called when a box is clicked. Here, "element" will hold the same value as "this" does in the HTML.
// "this" is a special word in JS but "element" could have been "thing" or "el" or whatever we wanted it to be as long as we use it again in the "console.log" statement
const handleClick = (element) => {

// this uses the "log" method on the "console" to log out the element's id so we can see it with our human eyes
console.log(`The element you clicked on has an id: ${element.id}`)
console.log(`The element you clicked on has an id: ${element.id}`);

// this next line prevents an X being changed to an O or an O being changed to an X by...
// checking to see if the square clicked has anything in it, if not continue
if(!document.getElementById(element.id).innerHTML){
addMarker(element.id)
if (!document.getElementById(element.id).innerHTML) {
addMarker(element.id);
}
}










};

// this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function.
const addMarker = (id) => {
// @TODO-1: Open the console tab in your Chrome Inspector Tool and click on the top-left square to see what's logged to the console.
console.log(`*** The current marker is: ${currentMarker}. ***`);
console.log(
`Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}`
);

// @TODO-1: Open the console tab in your Chrome Inspector Tool and click on the top-left square to see what's logged to the console.
console.log(`*** The current marker is: ${currentMarker}. ***`)
console.log(`Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}`)

// @TODO-2: Build a line of code that will set the innerHTML property of the element that was clicked to the "currentMarker"

// @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line:
// = currentMarker
// .getElementById(id)
// document
// .innerHTML

changeMarker()
}








// .innerHTML
document.getElementById(id).innerHTML = currentMarker;

changeMarker();
};

// This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X"
const changeMarker = () => {
if(currentMarker === "X"){
currentMarker = "O"
if (currentMarker === "X") {
currentMarker = "O";
} else {
currentMarker = "X"
currentMarker = "X";
}
}









};

// This "resetBoard" function is called when the user clicks on the "Restart" button.
const resetBoard = () => {

// @TODO-3: To make your "Restart" button work you'll need to build a line of code here that:
// collects all of the "td" elements into an HTML Collection: https://www.w3schools.com/jsref/dom_obj_htmlcollection.asp
// collects all of the "td" elements into an HTML Collection: https://www.w3schools.com/jsref/dom_obj_htmlcollection.asp

// @TODO-3.5: MIX & MATCH, You will need the following pieces of code to build that line:
// squares
// .getElementsByTagName("TD")
// =
// document
// const

// loops over the HTML Collection of TDs and clears out the Xs and Os
for (i=0; i < squares.length; i++) {
const squares = document.getElementsByTagName("td");

// loops over the HTML Collection of TDs and clears out the Xs and Os
for (i = 0; i < squares.length; i++) {
// will log out the id of each square as it loops over them.
console.log(squares[i].id)
console.log(squares[i].id);

// sets the innerHTML to null to replace the "X" or "O"
squares[i].innerHTML = null
}
}
squares[i].innerHTML = null;
}
};

console.log(`hello world`);
const arr1 = [1, 2, 3, 4];
console.log(arr1.reverse().join(""));
function reverseString(str) {
return str.split("").reverse().join("");
}
console.log(reverseString(`hello world`));