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

PPTX
Tic tac toe
PPTX
Tic toc game presentation
PPTX
Tic Tac Toe Java Development
PPT
Tic tac toe c++ programing
PPTX
Tic tac toe
PPTX
Tic tac toe game with graphics presentation
PPTX
PPT
Tic tac toe c++ project presentation
Tic tac toe
Tic toc game presentation
Tic Tac Toe Java Development
Tic tac toe c++ programing
Tic tac toe
Tic tac toe game with graphics presentation
Tic tac toe c++ project presentation

What's hot (20)

PPTX
Tic Tac Toe ppt
PPTX
게임 랭킹 ( Game Leader Board )
PPT
Tic tac toe on c++ project
PDF
이원, 온라인 게임 프로젝트 개발 결산 - 마비노기 개발 완수 보고서, NDC2011
PPTX
Introduction to game development
PDF
Game Design Fundamentals
PPTX
Top Esports Tournaments & Leagues
PPTX
Game Development Step by Step
PPTX
Ludo (snack-ladder) game project presentation
PPT
Scratch and pair programming
PDF
장재화, Replay system, NDC2011
PDF
게임업계에서 내가 하고 싶은 일 찾는 방법
PDF
Game Design Fundamentals: The Formal Elements
PPTX
NDC2012_마비노기 영웅전 카이 포스트모템_시선을 사로잡는 캐릭터 카이 그 시도와 성공의 구현 일지
PDF
Matt Ellis "Writing Allocation Free Code in C#"
PDF
메이플스토리 11년 장수 비결 분석 일반유저가 2014년에 씀
PPTX
[역기획]로스트아크_엘릭서 시스템
PDF
KPIs for Mobile Game Soft Launch
PPTX
IEnumerator란 무엇인가?
PDF
게임제작개론 : #9 라이브 서비스
Tic Tac Toe ppt
게임 랭킹 ( Game Leader Board )
Tic tac toe on c++ project
이원, 온라인 게임 프로젝트 개발 결산 - 마비노기 개발 완수 보고서, NDC2011
Introduction to game development
Game Design Fundamentals
Top Esports Tournaments & Leagues
Game Development Step by Step
Ludo (snack-ladder) game project presentation
Scratch and pair programming
장재화, Replay system, NDC2011
게임업계에서 내가 하고 싶은 일 찾는 방법
Game Design Fundamentals: The Formal Elements
NDC2012_마비노기 영웅전 카이 포스트모템_시선을 사로잡는 캐릭터 카이 그 시도와 성공의 구현 일지
Matt Ellis "Writing Allocation Free Code in C#"
메이플스토리 11년 장수 비결 분석 일반유저가 2014년에 씀
[역기획]로스트아크_엘릭서 시스템
KPIs for Mobile Game Soft Launch
IEnumerator란 무엇인가?
게임제작개론 : #9 라이브 서비스
Ad

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

PDF
Build tic tac toe with javascript (3:28)
PDF
Build a game la september 7
PDF
Tf itjsbagg
PDF
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-431 (1)
PDF
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-7-47 (1)
PDF
Build a Game with JavaScript
PDF
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-7-47 (2)
PDF
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-7-47
PDF
Build a Game with JavaScript
PDF
Build a Game with JavaScript
PDF
Build a Game with JavaScript
PPTX
JavaScript Advanced - Useful methods to power up your code
PDF
Intro to javascript (5:2)
PPTX
CSC PPT 13.pptx
PDF
Build a Game with JavaScript - Thinkful DC
PDF
Build A Game with JavaScript - LA - July 11
PDF
Jsgamesd-11.21.17
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
PPTX
Week 6 java script loops
PDF
Fewd week5 slides
Build tic tac toe with javascript (3:28)
Build a game la september 7
Tf itjsbagg
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-431 (1)
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-7-47 (1)
Build a Game with JavaScript
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-7-47 (2)
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-7-47
Build a Game with JavaScript
Build a Game with JavaScript
Build a Game with JavaScript
JavaScript Advanced - Useful methods to power up your code
Intro to javascript (5:2)
CSC PPT 13.pptx
Build a Game with JavaScript - Thinkful DC
Build A Game with JavaScript - LA - July 11
Jsgamesd-11.21.17
JavaScript / Web Engineering / Web Development / html + css + js/presentation
Week 6 java script loops
Fewd week5 slides
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Machine learning based COVID-19 study performance prediction
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Programs and apps: productivity, graphics, security and other tools
Diabetes mellitus diagnosis method based random forest with bat algorithm

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