How to end a Star Wars game with a score

I need some help with my students to end their Star Wars game when a score is reached.
Link to my game

What I expect to happen: When a score of 100 is reached, the game will end

What actually happens: Typo on my if statement. It says score is not defined
What I’ve tried: I’ve tried to define score as a variable what I need to know is what the score is defined as when you are adding and removing points.

Any help with this is greatly appreciated! I feel like there is information that I’m missing to code this correctly.

Thank you!

Michael

@michael.condotta Thanks for posting this question.
I looked at your program and a few things are missing for the end game to happen.

You need to declare a variable score. You will also need to update that score each time R2 gets a rebel pilot and a stormtrooper. An update function is needed to address anything related to the score. I have posted an example below.

var score = 0;

function whenGetRebelPilot() {
addPoints(100);
}
function addPoints(num) {
score = score + num;
if (score > 200 ) {
endGame(“win”);
}
}

Each time R2 gets a rebel pilot, the addPoints function is called. It passes the value of 100 into the function and then gets added to score. When score is greater than 200, the game ends with “Win”
You will also need a function to remove points.

Let us know if you need further assistance.

This particular hour of code exercise gives you a limited number of functions or custom blocks to use. The full Javascript language isn’t really available at this level of the challenge.

When you call the function endGame("win") your score is displayed for you and you get a winning message. If you call the function endGame("lose") your score is displayed and you get a lose message.

Most people building this puzzle will make touching the stormtrooper an immediate loss while touching the rebel is a win. The puffer pigs are for extra points.

@tstone @jdonwells Thank you both for your input. I tried to declare variables and create the update function as was mentioned but I was still getting several error messages.

It feels that @jdonwells input that the full Javascript language isn’t available aligns with the errors that I am receiving. I had this feeling all along.

Thank you both for your help. It is greatly appreciated by both myself and my students!

1 Like