Create PT Algorithms and "onEvent"

I have a handful of students who have an “onEvent” that incorporate variables, conditions, and math… which qualify as algorithms… which, to me, satisfies the algorithm requirement… but, they are discouraged from using “onEvent” as an algorithm… if they are discussion the algorithm within the “onEvent” and not the “onEvent” itself, does that work? Help! An example is below…

function gamescreen() {
onEvent(“skateboard”, “click”, function() {
setPosition(“skateboard”, randomNumber(50,280), randomNumber(50, 350));
total_score = total_score+scoreMultiplier;
setText(“total_score”, total_score);
});
onEvent(“background”, “click”, function() {
lives = lives-1;
setText(“total_lives”, lives);
if (lives==0) {
setScreen(“lose_screen”);
loose();
}
});
}

It is okay to use an onEvent as an algorithm. But the algorithm and one of the two sub-algorithms must have math/logic. Also, the structure of the code in your gamescreen() function is incorrect. The student should not put an onEvent inside a function. Correcting the format, the student should write it as:

function gamescreen() {
setPosition(“skateboard”, randomNumber(50,280), randomNumber(50, 350));
total_score = total_score+scoreMultiplier;
setText(“total_score”, total_score);
}

onEvent(“skateboard”, “click”, function() {
gamescreen();
});

Looking at the above corrected algorithm, I see two sub-algorithms (1. set the position of the skateboard to a random location and 2. update the score) with math/logic. Note that even though I am counting random number as math, it has been debated. To be on the safe side use more than just a random number generator for a math operation. The main algorithm (the onEvent) does not have any math/logic and hence in my opinion would lose the point for row 5.

The second onEvent in your example in my opinion may pass rows 4, 5, and 6. It has a main algorithm of checking if the person has clicked the background and update the number of lives. The sub algorithm would be to change the screen and then “loose()”. So the main algorithm could be the decrementing of lives, the sub algorithm could be checking if any lives are left, if not go to lose_screen. Much is left to the interpretation of the reader here. It would be much easier to interpret if the onEvent has function calls as shown below.

function updateScreenIfLost(){
if (lives == 0) {
setScreen(“lose_screen”);
}

function loose() {
…//I don’t know the code that goes here
}

onEvent(“background”, “click”, function() {
lives = lives - 1;
setText(“total_lives”, lives);
updateScreenIfLost();
loose();

I hope this helps.

@dmaletta The concept for your algorithm works. @bhatnagars is correct that there is an error in how the gamescreen() function is built. As she suggested it better to break each section apart and call each needed function. Just remember that the algorithm needs to consist of 2 or more smaller algorithms that include math and or logic.

function gamescreen() {
//your main algorithm function
//conditional
addPoints()
loose()
}
function addPoints() {
//add points for an activity
}
function loose() {
//loose points for an activity
}

I have encouraged my students to include logic and math in all their algorithms.

1 Like