First to Twenty
Build the game.
First to Twenty is a free Javascript for Kids lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Javascript for Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
First to Twenty Wins! 🎯
Time to build a real game: First to Twenty! 🎯
Players take turns rolling. The first to reach 20 points wins! 🏆
console.log('First to 20 wins! 🎯')
console.log('Keep rolling! 🎲')The Goal 🏁
Let's set the goal to 20. We keep rolling until our score reaches it! 🏁
We store the goal in a box so it's easy to change later. 📦
let goal = 20
console.log('Goal: reach ' + goal + ' points! 🎯')Start the Score 📦
Every game starts at 0 points. 📦
We'll roll and add until we hit the goal! 🎲
let score = 0
let goal = 20
console.log('Score: ' + score + ' / Goal: ' + goal)Roll Until Twenty 🔄
A while loop is perfect! It keeps rolling while the score is below 20. 🔄
When we hit 20 or more, the loop stops! 🛑
let score = 0
while (score < 20) {
score += Math.floor(Math.random() * 6) + 1
}
console.log('Reached ' + score + '! 🎉')Show Each Roll 🎲
Let's print each roll so we can watch the game! 🎲
So exciting to see the score climb! 📈
let score = 0
while (score < 20) {
let roll = Math.floor(Math.random() * 6) + 1
score += roll
console.log('Rolled ' + roll + ', score is now ' + score)
}
console.log('Winner! 🏆')Two Players Take Turns 👥
Now let's add player 2! Both start at 0. 👥
We'll let them race to 20! 🏁
let player1 = 0
let player2 = 0
player1 += Math.floor(Math.random() * 6) + 1
player2 += Math.floor(Math.random() * 6) + 1
console.log('P1: ' + player1 + ', P2: ' + player2 + ' 🎲')Both Race to Twenty 🏁
We keep rolling for both players until one reaches 20! 🏁
We use OR || to stop when either one wins. 🔀
let p1 = 0
let p2 = 0
while (p1 < 20 && p2 < 20) {
p1 += Math.floor(Math.random() * 6) + 1
p2 += Math.floor(Math.random() * 6) + 1
}
console.log('Final: P1=' + p1 + ' P2=' + p2 + ' 🎲')Find the Winner 🏆
When the rolling stops, we check who reached 20 first! 🏆
Use if to find the champion! 🎉
let p1 = 21
let p2 = 18
if (p1 >= 20) {
console.log('Player 1 wins! 🏆')
} else {
console.log('Player 2 wins! 🏆')
}The Whole Game 🕹️
Let's put EVERYTHING together into one game! 🕹️
Roll, add, check, and announce the winner! 🎉
let p1 = 0
let p2 = 0
while (p1 < 20 && p2 < 20) {
p1 += Math.floor(Math.random() * 6) + 1
p2 += Math.floor(Math.random() * 6) + 1
}
console.log('P1: ' + p1 + ', P2: ' + p2)
if (p1 >= 20) console.log('Player 1 wins! 🏆')
else console.log('Player 2 wins! 🏆')Make It Yours! 🎨
Now you can change the game! 🎨
- Change the goal to 30 or 50 🎯
- Add bonus points for sixes 🎁
- Add a third player! 👥
It's your game now! 😎
let goal = 30
console.log('New goal: ' + goal + '! 🎯')
console.log('Make the game your own! 🎨')You Built a Full Game! 🕹️
Wow! You built the whole First to Twenty game! 🎉
- A goal to reach 🎯
- A
whileloop to keep rolling 🔄 - Scores for each player 🏅
- A winner check 🏆
console.log('You built a game! 🕹️')
console.log('You are amazing! 🌟')Quick Check 🧠
Last brain question! 🤔
You Did It! 🎉
You finished the Roll the Dice course! 🥳
- Rolled one die 🎲
- Rolled two dice and added them 🎲🎲
- Tracked points 🏅
- Built First to Twenty 🎯
You are a real game maker! 🌟
console.log('Course complete! 🎓')
console.log('Great rolling! 🎲🎉')Frequently asked questions
Is the “First to Twenty” lesson free?
Yes — the full text of “First to Twenty” is free to read here on the web, and the Javascript for Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Javascript for Kids course, upgrade to CoddyKit PRO.
What will I learn in “First to Twenty”?
Build the game. You practise Javascript for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Javascript for Kids?
No prior experience is required. Javascript for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “First to Twenty” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Javascript for Kids lesson?
Yes. Every Javascript for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Rolling a Die
- Two Dice
- Tracking Dice Points
- First to Twenty