Skip to main content
Participating Frequently
March 20, 2022
Question

Calling a repeated set of instructions in a function.

  • March 20, 2022
  • 2 replies
  • 244 views

I am writing a quiz for my students. There are set of question marks that I keep needing to hide.

Here are the instructions that work.

 this.qmmcm.visible=false;

this.qcmm.visible=false;

this.qmkm.visible=false;

this.qcmmm.visible=false;

 

I have written these into a function like this:

function hidequestionmarks();{

   this.qmmcm.visible=false;

   this.qcmm.visible=false;

   this.qmkm.visible=false;

    this.qcmmm.visible=false;

}

But when I try to call these from another function, it doesn't work.

I use the instruction 

hidequestionmarks();

in the other function.

 

Can someone given me some guidance as to what I am doing wrong.

 

Thank you

Rod

This topic has been closed for replies.

2 replies

Legend
March 20, 2022

Pass "this" as an argument to the function.

JoãoCésar17023019
Community Expert
Community Expert
March 20, 2022

Hi.

 

Can you give us an example of how/where you calling your function?

 

Regards,

JC

rodecssAuthor
Participating Frequently
March 20, 2022

//I have extracted this from the code.

//This function hides a set of 6 question marks that I use in the game.

 

function hidequestionmarks() {
this.qmmcm.visible = false;
this.qcmm.visible = false;
this.qmkm.visible = false;
this.qcmmm.visible = false;
this.qmcm.visible = false;
this.qkmm.visible = false;
}

//now all I want to do is to call this function when I click a button to show just one question mark behind which there will be an answer for the students.

// you will notice that I have repeated the instructions to hide all the questionmarks. I would like to understand how I can call the function hidequestionamrks() instead so that I can reuse the code in other parts of the game.

 

numholder = 0
function go() {
//this.addEventListener("click", hidequestionmarks.bind(this)); //this is so cool
this.qmmcm.visible = false;
this.qcmm.visible = false;
this.qmkm.visible = false;
this.qcmmm.visible = false;
this.qmcm.visible = false;
this.qkmm.visible = false;
this.cmmm.text = "";
this.mcm.text = "";
this.kmm.text = "";
this.mmcm.text = "";
this.cmm.text = "";
this.mkm.text = "";
do {
next = randomIntFromInterval(1, 6)
} while (next == numholder)

numholder = next;
switch (next) {
case 1:
this.qcmmm.visible = true;
break;
case 2:
this.qmcm.visible = true;
break;
case 3:
this.qkmm.visible = true;
break;
case 4:
this.qmmcm.visible = true;
break;
case 5:
this.qcmm.visible = true;
break;
case 6:
this.qmkm.visible = true;
break;
}

}