Debugging Student Code Unit 5 Lesson 3

This is some of the code from a student’s project, the object of the code is to capture how many hits he has on a game. The problem is the code is not generating the right information. Any ideas would be greatly appreciated.

Here’s the code:

var score;
var moleArray = [“mole1”, “mole2”, “mole3”, “mole4”, “mole5”, “mole6”, “mole7”, “mole8”, “mole9”, “mole10”];

setScreen (“welcomeScreen”);

onEvent(“playButton”, “click”, function(){
setProperty(“mole1”, “hidden”, true);
setProperty(“mole2”, “hidden”, true);
setProperty(“mole3”, “hidden”, true);
setProperty(“mole4”, “hidden”, true);
setProperty(“mole5”, “hidden”, true);
setProperty(“mole6”, “hidden”, true);
setProperty(“mole7”, “hidden”, true);
setProperty(“mole8”, “hidden”, true);
setProperty(“mole9”, “hidden”, true);
setProperty(“mole10”, “hidden”, true);

function updateScreen(){
setText(“total_score”, score);
function displayMole(){
var num = (Math.random() * 9);
document.canvas.src = moleArray[num];

//test button
onEvent (“genMole”, “click”, function(){

//clicking mole
onEvent (“mole1”, “click”, function(){
setProperty(“mole1”, “hidden”, true);
score = score + 1;

Student has used various websites that lead to incorrect functions to be used in the Design Mode. High School Freshman.

Original Link:

Unit 5 CSP Lesson 3

Student has not tried anything to repair code.

The app is supposed to allow users to whack a mole, the program uses a button that has to be clicked several times. The student would like the app to record how many hits a player takes before they win the game. The app should record the number of hits and refresh the score on the screen. Right now, it does nothing.

Welcome to the CSP online community!

Could you please provide the following details?

  • Share a link to the project / level that’s giving them trouble
  • A description of what specifically you expect to happen, ideally talking about a specific element on the screen or specific lines of code
  • A description of what happens instead
  • A description of what you already have tried to solve the problem
  • The more details the better

The first problem I see is on line 32 in the click event of genMole. In order to call the displayMole method, the student needs parenthesis after the method name - displayMole();

Then I’m not sure what the student is trying to do in line 26. That is giving an error. I don’t see any canvas in the project. I think what the student should do is simply “show” the mole corresponding to the random number generated using the showElement() method. The student should also probably use randomNumber(0,9) function instead of Math.random() to get a random integer between 0 and 9.