• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Calling a repeated set of instructions in a function.

Community Beginner ,
Mar 20, 2022 Mar 20, 2022

Copy link to clipboard

Copied

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

TOPICS
Code

Views

131

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2022 Mar 20, 2022

Copy link to clipboard

Copied

Hi.

 

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

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 20, 2022 Mar 20, 2022

Copy link to clipboard

Copied

LATEST

//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;
}

}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 20, 2022 Mar 20, 2022

Copy link to clipboard

Copied

Pass "this" as an argument to the function.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines