SlideShare a Scribd company logo
Build Tic Tac Toe in Javascript
March 2017
bit.ly/thinkful-tictactoe
About me
• David Coulter
• Frontend developer and product manager for 10 years at
National Geographic, NGP Van, Arcadia Power
• Mentor @ Thinkful
About us
Thinkful prepares students for web development &
data science jobs with 1-on-1 mentorship programs
Programming level (Javascript)
• First lines of code written tonight
• I’ve been coding for a couple months
• I’ve been coding for awhile
Notes for tonight
• Goal is to provide exposure to a more complex
challenge than we normally face as a beginner
• Beginner-friendly, but will be challenging if you’re
completely new to code
• You might only get the answer to one or two
challenges, but we’ll walk through every line of
code and learn through the exposure
• Don’t give up, try to struggle for the answer
• We’ll circulate and answer questions while we’re
working on challenges
Format for tonight
• Crash course on a concept
• See how it is implemented in the code
• Try a challenge in the starter code
User story
• Opens to a blank 3 x 3 grid
• Player 1 clicks on a square, fills in an “X”
• Player 2 clicks on another square, fills in an “O”
• Alert pops up when a player wins, restarts game
• If no one wins, alert pops up, restarts game
Creating the board
HTML overview
HTML elements usually consist of an opening tag, closing tag,
and some content. These elements also have attributes.
<table border=“1”> #html element starts here
<tr> #body element starts here
<td height=“50” width=“50”></td>
#this is an HTML element
</tr> #body element ends here
</table> #html element ends here
Starter code
• We’ve done this for you
• Everything from now on will be Javascript!
• JSFiddle: http://bit.ly/tic-tac-toe-starter-code
Note on different approaches
• There are many ways to solve complex coding
problems
• Your job is to consider trade-offs and choose the most
effective solution (the meaning of effective depends on
circumstances)
• As a beginner, your job is to make it work, and learn in
the process
• We’re going to cut through a lot of those tradeoffs and
make specific choices for how to design the program
Our approach
1 2 4
8 16 32
64 128 256
Each square is given a value so that if you add up any
combination of values, you’ll get a different number
Our approach
1 2 4
8 16 32
64 128 256
After each turn, we’ll add up the “score” for each player to
see if they win (i.e. add up to a winning number)
7
56
84
448
273
29214673
Javascript 101 — Variables
• A variable is a name that is attached to a value
• Gives us a shorthand way to refer to it elsewhere
• Can be string, number, boolean, objects, and arrays…
• Variables also help us manage state (turn = “X”)
var firstVariable = “hello world”;
Challenge: What variables do we need?
We’ll be using three main variables throughout the
program to “manage state”. What do we want to
remember for the whole program?
Can you figure out what these will need to be?
Answer
These variables track where players are in the game
Javascript Arrays
• With arrays we can store a list of items in a single variable
• We use “brackets” to refer to an array in our code
• The stored item can be a number, string, array, object, etc…
• Each array is “indexed” and starts with 0, then 1, etc
• var firstItem = wins[0] saves 5 to firstItem variable
• What will wins[2] return?
var wins = [5, 10, 15];
Challenge: Fill in “wins” variable
In your starter code, set the win variable to an array with
the list of winning scores
Answer
wins = [7, 56, 84, 73, 146, 273, 292, 448]
Adding a second array…
<td> <td> <td>
<td> <td> <td>
<td> <td> <td>
[<td>,<td>,<td>,<td>,<td>,<td>,<td>,<td>,<td>]
Javascript Functions
function doSomething () {
alert(“it’s done”);
};
doSomething();
A function lets you separate your code into bite-sized
pieces which you can use over again.
When you write a function to use later, you are
“declaring” it. When you use (or run) that function you
are “calling” it.
Javascript Functions — Parameters and Return
• You can also send a “parameter” to the function (the input)
• You can also “return” a value from the function (the output)
function doSomething (string) {
return string;
};
doSomething(“Hi”);
Challenge: “Call” the start new game function
In the init_game() function, call the the start new
game function (replace TODO in init_game)
Answer
What do we need to do inside start_new_game
• Set who goes first
• Reset the “number_of_moves” to 0
• Reset the scores (stored in scoreXO)
• Clear the boxes of any X’s and O’s
• Let’s do it together on the next slide
start_new_game
Adding click “listeners” to the boxes
Javascript “for” loops
• Used to run same code again and again with different values
• Parameter #1: Executed before the loop starts
• Parameter #2: Defines the condition for running the loop
• Parameter #3: Executed after the loop is done
• This loop will print “Javascript!” five times
for (var i = 0; i < 5; i++) {
console.log(“Javascript!”)
};
Javascript If/Else Statements
• Enables program to do different things based on conditions
• You can use if, else if or else
• If the statement in parentheses is true, it will execute that block
• Otherwise the program will move to the next block
if (turn == “O”) {
console.log(“It’s O’s turn”);
} else if (turn == “X”) {
console.log(“It’s X’s turn”);
} else {
console.log(“It’s no one’s turn”);
}
First For Loop when user clicks
“For” each square, we need to:
• Register that the click happened with a click event
• Get the number that’s attached to the square
• If it’s X’s turn, add to the score for X; if O’s turn, add to the score
for Y
• Add 1 to the number of moves
• Set the content of the square to show X or O
• Let’s walk through this
First For Loop
Question
After each move, what are the three possible outcomes?
Challenge (even trickier!)
Answer
Another challenge
Answer
Final challenge (maybe) — run the program!
Answer
Bonus! There’s more!
Bonus Solution
More about Thinkful
• Anyone who’s committed can learn to code
• 1-on-1 mentorship is the best way to learn
• Flexibility matters — learn anywhere, anytime
Our Program
You’ll learn concepts, practice with drills, and build capstone
projects for your own portfolio — all guided by a personal mentor
Our Mentors
Mentors have, on average, 10 years of experience
Our Results
Job Titles after GraduationMonths until Employed
Special Introductory Offer
• Two-week program, includes six mentor sessions for $50
• Starts with HTML/CSS/Javascript
• Option to continue into full web development bootcamp
• Talk to me (or email me) if you’re interested
October 2015
Questions?
dcoulter@thinkful.com
dan@thinkful.com
schedule a call through thinkful.com

More Related Content

What's hot

project on snake game in c language
project on snake game in c languageproject on snake game in c language
project on snake game in c language
Ashutosh Kumar
 
Tic tac toe c++ project presentation
Tic tac toe c++ project presentationTic tac toe c++ project presentation
Tic tac toe c++ project presentationSaad Symbian
 
Tic Tac Toe ppt
Tic Tac Toe pptTic Tac Toe ppt
Tic Tac Toe ppt
SanchitRastogi15
 
Tic Tac Toe Project
Tic Tac Toe Project Tic Tac Toe Project
Tic Tac Toe Project
KanikaJawla
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
Tanvir360
 
Final project report Snake Game in Python
Final project report Snake Game in PythonFinal project report Snake Game in Python
Final project report Snake Game in Python
Muhammad Aziz
 
Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ project
Utkarsh Aggarwal
 
Android application - Tic Tac Toe
Android application - Tic Tac ToeAndroid application - Tic Tac Toe
Android application - Tic Tac Toe
Sarthak Srivastava
 
Snake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malavSnake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malav
Rohit malav
 
Snake Game in Python Progress report
Snake Game in Python Progress reportSnake Game in Python Progress report
Snake Game in Python Progress report
Muhammad Aziz
 
FPS GAME FYP Documentation
FPS GAME FYP DocumentationFPS GAME FYP Documentation
FPS GAME FYP Documentation
Danial Ahmed
 
report on snake game
report on snake game report on snake game
report on snake game
azhar niaz
 
Tic tac toe c++ programing
Tic tac toe c++ programingTic tac toe c++ programing
Tic tac toe c++ programing
Krishna Agarwal
 
Game Development Step by Step
Game Development Step by StepGame Development Step by Step
Game Development Step by Step
Bayu Sembada
 
Variables in python
Variables in pythonVariables in python
Variables in python
Jaya Kumari
 
Game Design - Lecture 1
Game Design - Lecture 1Game Design - Lecture 1
Game Design - Lecture 1
Andrea Resmini
 
MIND GAME ZONE - Abhijeet
MIND GAME ZONE - AbhijeetMIND GAME ZONE - Abhijeet
MIND GAME ZONE - Abhijeet
Abhijeet Kalsi
 
Car racing game for android
Car racing game for androidCar racing game for android
Car racing game for android
ravijot singh
 
Final Year Game Project Presentation
Final Year Game Project Presentation Final Year Game Project Presentation
Final Year Game Project Presentation
Nusrat Jahan Shanta
 
1-Introduction (Game Design and Development)
1-Introduction (Game Design and Development)1-Introduction (Game Design and Development)
1-Introduction (Game Design and Development)
Hafiz Ammar Siddiqui
 

What's hot (20)

project on snake game in c language
project on snake game in c languageproject on snake game in c language
project on snake game in c language
 
Tic tac toe c++ project presentation
Tic tac toe c++ project presentationTic tac toe c++ project presentation
Tic tac toe c++ project presentation
 
Tic Tac Toe ppt
Tic Tac Toe pptTic Tac Toe ppt
Tic Tac Toe ppt
 
Tic Tac Toe Project
Tic Tac Toe Project Tic Tac Toe Project
Tic Tac Toe Project
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
 
Final project report Snake Game in Python
Final project report Snake Game in PythonFinal project report Snake Game in Python
Final project report Snake Game in Python
 
Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ project
 
Android application - Tic Tac Toe
Android application - Tic Tac ToeAndroid application - Tic Tac Toe
Android application - Tic Tac Toe
 
Snake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malavSnake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malav
 
Snake Game in Python Progress report
Snake Game in Python Progress reportSnake Game in Python Progress report
Snake Game in Python Progress report
 
FPS GAME FYP Documentation
FPS GAME FYP DocumentationFPS GAME FYP Documentation
FPS GAME FYP Documentation
 
report on snake game
report on snake game report on snake game
report on snake game
 
Tic tac toe c++ programing
Tic tac toe c++ programingTic tac toe c++ programing
Tic tac toe c++ programing
 
Game Development Step by Step
Game Development Step by StepGame Development Step by Step
Game Development Step by Step
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Game Design - Lecture 1
Game Design - Lecture 1Game Design - Lecture 1
Game Design - Lecture 1
 
MIND GAME ZONE - Abhijeet
MIND GAME ZONE - AbhijeetMIND GAME ZONE - Abhijeet
MIND GAME ZONE - Abhijeet
 
Car racing game for android
Car racing game for androidCar racing game for android
Car racing game for android
 
Final Year Game Project Presentation
Final Year Game Project Presentation Final Year Game Project Presentation
Final Year Game Project Presentation
 
1-Introduction (Game Design and Development)
1-Introduction (Game Design and Development)1-Introduction (Game Design and Development)
1-Introduction (Game Design and Development)
 

Similar to Build tic tac toe with javascript (4:11 dc)

Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)
Thinkful
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)
Thinkful
 
Intro to javascript (6:27)
Intro to javascript (6:27)Intro to javascript (6:27)
Intro to javascript (6:27)
David Coulter
 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)
Thinkful
 
Thinkful - Intro to JavaScript
Thinkful - Intro to JavaScriptThinkful - Intro to JavaScript
Thinkful - Intro to JavaScript
TJ Stalcup
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)
Thinkful
 
Flow charts week 5 2020 2021
Flow charts week 5 2020  2021Flow charts week 5 2020  2021
Flow charts week 5 2020 2021
Osama Ghandour Geris
 
Flowcharting week 5 2019 2020
Flowcharting week 5  2019  2020Flowcharting week 5  2019  2020
Flowcharting week 5 2019 2020
Osama Ghandour Geris
 
Java Script web development - Engineering
Java Script web development - EngineeringJava Script web development - Engineering
Java Script web development - Engineering
DineshKumar A
 
Test driven development
Test driven developmentTest driven development
Test driven development
christoforosnalmpantis
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
Akash Pandey
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
Jeremy Likness
 
Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017
Thinkful
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
vitalikaush1
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Fwdays
 
TDD: seriously, try it! 
TDD: seriously, try it! TDD: seriously, try it! 
TDD: seriously, try it! 
Nacho Cougil
 
Mutation testing with PIT
Mutation testing with PITMutation testing with PIT
Mutation testing with PIT
Rafał Leszko
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
kankemwa Ishaku
 

Similar to Build tic tac toe with javascript (4:11 dc) (20)

Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)
 
Intro to javascript (6:27)
Intro to javascript (6:27)Intro to javascript (6:27)
Intro to javascript (6:27)
 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)
 
Thinkful - Intro to JavaScript
Thinkful - Intro to JavaScriptThinkful - Intro to JavaScript
Thinkful - Intro to JavaScript
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)
 
Flow charts week 5 2020 2021
Flow charts week 5 2020  2021Flow charts week 5 2020  2021
Flow charts week 5 2020 2021
 
Flowcharting week 5 2019 2020
Flowcharting week 5  2019  2020Flowcharting week 5  2019  2020
Flowcharting week 5 2019 2020
 
Java Script web development - Engineering
Java Script web development - EngineeringJava Script web development - Engineering
Java Script web development - Engineering
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
 
TDD: seriously, try it! 
TDD: seriously, try it! TDD: seriously, try it! 
TDD: seriously, try it! 
 
Mutation testing with PIT
Mutation testing with PITMutation testing with PIT
Mutation testing with PIT
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 

Recently uploaded

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 

Recently uploaded (20)

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 

Build tic tac toe with javascript (4:11 dc)

  • 1. Build Tic Tac Toe in Javascript March 2017 bit.ly/thinkful-tictactoe
  • 2. About me • David Coulter • Frontend developer and product manager for 10 years at National Geographic, NGP Van, Arcadia Power • Mentor @ Thinkful
  • 3. About us Thinkful prepares students for web development & data science jobs with 1-on-1 mentorship programs
  • 4. Programming level (Javascript) • First lines of code written tonight • I’ve been coding for a couple months • I’ve been coding for awhile
  • 5. Notes for tonight • Goal is to provide exposure to a more complex challenge than we normally face as a beginner • Beginner-friendly, but will be challenging if you’re completely new to code • You might only get the answer to one or two challenges, but we’ll walk through every line of code and learn through the exposure • Don’t give up, try to struggle for the answer • We’ll circulate and answer questions while we’re working on challenges
  • 6. Format for tonight • Crash course on a concept • See how it is implemented in the code • Try a challenge in the starter code
  • 7. User story • Opens to a blank 3 x 3 grid • Player 1 clicks on a square, fills in an “X” • Player 2 clicks on another square, fills in an “O” • Alert pops up when a player wins, restarts game • If no one wins, alert pops up, restarts game
  • 9. HTML overview HTML elements usually consist of an opening tag, closing tag, and some content. These elements also have attributes. <table border=“1”> #html element starts here <tr> #body element starts here <td height=“50” width=“50”></td> #this is an HTML element </tr> #body element ends here </table> #html element ends here
  • 10. Starter code • We’ve done this for you • Everything from now on will be Javascript! • JSFiddle: http://bit.ly/tic-tac-toe-starter-code
  • 11. Note on different approaches • There are many ways to solve complex coding problems • Your job is to consider trade-offs and choose the most effective solution (the meaning of effective depends on circumstances) • As a beginner, your job is to make it work, and learn in the process • We’re going to cut through a lot of those tradeoffs and make specific choices for how to design the program
  • 12. Our approach 1 2 4 8 16 32 64 128 256 Each square is given a value so that if you add up any combination of values, you’ll get a different number
  • 13. Our approach 1 2 4 8 16 32 64 128 256 After each turn, we’ll add up the “score” for each player to see if they win (i.e. add up to a winning number) 7 56 84 448 273 29214673
  • 14. Javascript 101 — Variables • A variable is a name that is attached to a value • Gives us a shorthand way to refer to it elsewhere • Can be string, number, boolean, objects, and arrays… • Variables also help us manage state (turn = “X”) var firstVariable = “hello world”;
  • 15. Challenge: What variables do we need? We’ll be using three main variables throughout the program to “manage state”. What do we want to remember for the whole program? Can you figure out what these will need to be?
  • 16. Answer These variables track where players are in the game
  • 17. Javascript Arrays • With arrays we can store a list of items in a single variable • We use “brackets” to refer to an array in our code • The stored item can be a number, string, array, object, etc… • Each array is “indexed” and starts with 0, then 1, etc • var firstItem = wins[0] saves 5 to firstItem variable • What will wins[2] return? var wins = [5, 10, 15];
  • 18. Challenge: Fill in “wins” variable In your starter code, set the win variable to an array with the list of winning scores
  • 19. Answer wins = [7, 56, 84, 73, 146, 273, 292, 448]
  • 20. Adding a second array… <td> <td> <td> <td> <td> <td> <td> <td> <td> [<td>,<td>,<td>,<td>,<td>,<td>,<td>,<td>,<td>]
  • 21. Javascript Functions function doSomething () { alert(“it’s done”); }; doSomething(); A function lets you separate your code into bite-sized pieces which you can use over again. When you write a function to use later, you are “declaring” it. When you use (or run) that function you are “calling” it.
  • 22. Javascript Functions — Parameters and Return • You can also send a “parameter” to the function (the input) • You can also “return” a value from the function (the output) function doSomething (string) { return string; }; doSomething(“Hi”);
  • 23. Challenge: “Call” the start new game function In the init_game() function, call the the start new game function (replace TODO in init_game)
  • 25. What do we need to do inside start_new_game • Set who goes first • Reset the “number_of_moves” to 0 • Reset the scores (stored in scoreXO) • Clear the boxes of any X’s and O’s • Let’s do it together on the next slide
  • 28. Javascript “for” loops • Used to run same code again and again with different values • Parameter #1: Executed before the loop starts • Parameter #2: Defines the condition for running the loop • Parameter #3: Executed after the loop is done • This loop will print “Javascript!” five times for (var i = 0; i < 5; i++) { console.log(“Javascript!”) };
  • 29. Javascript If/Else Statements • Enables program to do different things based on conditions • You can use if, else if or else • If the statement in parentheses is true, it will execute that block • Otherwise the program will move to the next block if (turn == “O”) { console.log(“It’s O’s turn”); } else if (turn == “X”) { console.log(“It’s X’s turn”); } else { console.log(“It’s no one’s turn”); }
  • 30. First For Loop when user clicks “For” each square, we need to: • Register that the click happened with a click event • Get the number that’s attached to the square • If it’s X’s turn, add to the score for X; if O’s turn, add to the score for Y • Add 1 to the number of moves • Set the content of the square to show X or O • Let’s walk through this
  • 32. Question After each move, what are the three possible outcomes?
  • 37. Final challenge (maybe) — run the program!
  • 41. More about Thinkful • Anyone who’s committed can learn to code • 1-on-1 mentorship is the best way to learn • Flexibility matters — learn anywhere, anytime
  • 42. Our Program You’ll learn concepts, practice with drills, and build capstone projects for your own portfolio — all guided by a personal mentor
  • 43. Our Mentors Mentors have, on average, 10 years of experience
  • 44. Our Results Job Titles after GraduationMonths until Employed
  • 45. Special Introductory Offer • Two-week program, includes six mentor sessions for $50 • Starts with HTML/CSS/Javascript • Option to continue into full web development bootcamp • Talk to me (or email me) if you’re interested