A Simple Javascript Problem

Hi @MarkHunte Sorry for the confusion.

So a user could enter
var vStepAnswer = 1 /// mock of the step feed back
var wStepAnswer = 1 /// mock of the step feed back
var xStepAnswer = 1/// mock of the step feed back

… and currently the result would be: “Great job! You have entered all of the correct notes.” These answers are all possible correct answers. However, for the solution to be deemed correct the user must enter all 3 of the correct answers without repeating any. Like this:

var vStepAnswer = 1 /// mock of the step feed back
var wStepAnswer = 3 /// mock of the step feed back
var xStepAnswer = 5/// mock of the step feed back

It does not matter which answer holds the 1, 3 or 5 but they must all be entered.

So you mean that they must enter all 3 numbers for each answer rather than just get 1 number for each answer correct.

i.e

They need to enter 1,6 and 2 for the single correct answer [1,6 ,2] if they enter 1,6,4 then the answer is wrong.

If this is right I will assume you already have code to put the feedback into an array for each answer.
i.e feed back =
var vStepAnswer = [2 , 4, 0] /// mock of the step feed back

Not exactly. There are three different correct answers to be placed in 3 different places. It does not matter in which place they put a correct answer but they must include all 3 different correct answers and not repeat any of them.

Yes, I have put the feedback into an array for each answer, for example
var vStepAnswer = [1,3,5]

and there are 3 variables like:
var vStepAnswer = [1,3,5]
var wStepAnswer = [1,3,5]
var xStepAnswer = [1,3,5]

does the order of numbers in the answer matter.

i.e must it be [1,3,5] or could it be [5,1,3]

0I am hope it does then I would look at just concatenating the numbers into a single answer..

i.e [1,3,5] goes concatenated to 135, which would be easier to compare.

Like this.

 var vStepAnswer = [2 , 4, 0].join("")   /// mock of the step feed back
    	var wStepAnswer = [3,5 ,1].join("")  /// mock of the step feed back
    	var xStepAnswer = [ 1,3,6].join("") /// mock of the step feed back
    	
    	
    	var stepKeysAnswers  = [['vAnswer',vStepAnswer,"E"],['wAnswer',wStepAnswer,"F"],['xAnswer',xStepAnswer,"D"]]// we put the feed back answers in arrays with the correct answer key name.
      
     
       var  correctAnswers = {vAnswer:["240"],wAnswer:["351"],xAnswer:[ "136"] } //-- all correct answers

Unfortunately the order does not matter. And it is integral that the answers could come in any order.

Maybe some simple sort foo…

var vStepAnswer = [2 , 4, 1].sort().join("")   /// mock of the step feed back
var wStepAnswer = [3,5 ,1].sort().join("")  /// mock of the step feed back
var xStepAnswer = [ 1,3,6].sort().join("") /// mock of the step feed back


    var stepKeysAnswers  = [['vAnswer',vStepAnswer,"E"],['wAnswer',wStepAnswer,"F"],['xAnswer',xStepAnswer,"D"]]// we put the feed back answers in arrays with the correct answer key name.
  
 
   var  correctAnswers = {vAnswer:["124"],wAnswer:["135"],xAnswer:[ "136"] } //-- all correct answers
     
 
    var answerCheckIncorrect = []; //-- final array to hold the false results below.

Would have to play with the sort if you wanted double digits and anything past 9,
The sort (in this case ) orders the numbers by greater value.

var vStepAnswer = [2 , 4, 1].sort().join("")  
--> "124"

The ones in the correctAnswers would need to match that.
i.e 1 through 9 left to right.


Also you could have the code run on individual answer/feedback and store if correct or not for later when all answers are ready to be displayed

1 Like

This is great! I will try it. And, no double digits needed. Answers are 0 thru 6.

Thank you!
Matt

Thanks @MarkHunte I’m not able to get this to work and I think it might be because I’m placing the .sort().join in the wrong place or not doing the correct things with it.

My original code was this:

	window.scoreView.getScore().done(function(scoreData) {
	
	
 
	

	
	var vStepAnswer = scoreData.staves[0].measures[0].noteSets[0].notes[0].step;
	var wStepAnswer = scoreData.staves[1].measures[0].noteSets[0].notes[0].step;
	var xStepAnswer = scoreData.staves[2].measures[0].noteSets[0].notes[0].step;
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	

	
	var stepKeysAnswers  = [['vAnswer',vStepAnswer,"Clarinet"],['wAnswer',wStepAnswer,"Trumpet"],['xAnswer',xStepAnswer,"Violin"]]// we put the feed back answers in arrays with the correct answer key name.
  
 
   var  correctAnswers = {vAnswer:[1,3,5],wAnswer:[1,3,5],xAnswer:[1,3,5] } //-- all correct answers
     
 
    var answerCheckIncorrect = []; //-- final array to hold the false results below.
    
    
    
     
for (i = 0; i < stepKeysAnswers.length; i++) { 
//-- we iterate over each setp answer array and work through each answer using the key and feed back answer for that key
 

 var thisIsCorrect =   correctAnswers[stepKeysAnswers[i][0]].includes(stepKeysAnswers[i][1]) //- i.e correctAnswers[wAnswer].includes(4)
 
 // push the true or false result to the answerCheck array.
 if (!thisIsCorrect){
 
 answerCheckIncorrect.push(stepKeysAnswers[i]);
 
 
 }
}
  
//console.log(answerCheckIncorrect);		
 	
 

   //-- All Correct
if (answerCheckIncorrect.length == 0 ){   
	
	 hypeDocument.getElementById("feedback6").innerHTML = "Great job! You have entered the notes D, F and A. These are the notes of D minor which is the ii chord.";
         hypeDocument.getElementById("feedback6").style.color = "#008F00";
         hypeDocument.getElementById("feedback6").style.borderLeft =  "6px solid green";  
         hypeDocument.getElementById("check4").innerHTML = "Play Again!";
         
         
         window.scoreView.playFromMeasure(0); 
        
        
        window.scoreView.addEventListener('playbackStop', function(event) {
        
        
        
       hypeDocument.getElementById('next7').style.display="block";
        });
         
         
       return;  
       
   }
   
   
   //-- One wrong
   if (answerCheckIncorrect.length == 1 ){   
	
	
		
	 hypeDocument.getElementById("feedback6").innerHTML = "The note in the  " + answerCheckIncorrect[0][2] + " is incorrect. The other notes are all correct. Try again!";
 hypeDocument.getElementById("feedback6").style.color = "red";
         hypeDocument.getElementById("feedback6").style.borderLeft =  "6px solid red";
         hypeDocument.getElementById("check4").innerHTML = "Check Again!"; 
         
         
         
       return;  
       
   }
   
    	//-- All wrong
 	if (answerCheckIncorrect.length == stepKeysAnswers.length ){
 	
 	    hypeDocument.getElementById("feedback6").innerHTML = "Try again! All of the notes you've entered are incorrect. ";
          hypeDocument.getElementById("feedback6").style.color = "red";
         hypeDocument.getElementById("feedback6").style.borderLeft =  "6px solid red";
         hypeDocument.getElementById("check4").innerHTML = "Check Again!"; 
         
         return;
   }
   


   //-- some wrong.
  var theNoticeString = "" 
  
  
for (i = 0; i < answerCheckIncorrect.length; i++) { 
  
 
 theNoticeString +=  " " + answerCheckIncorrect[i][2] + ", " ;

}
   
theNoticeString = "The notes you've entered in the  "  +     theNoticeString  + " are wrong. The other note is correct. Try again!";  

 hypeDocument.getElementById("feedback6").innerHTML = theNoticeString;
          hypeDocument.getElementById("feedback6").style.color = "red";
         hypeDocument.getElementById("feedback6").style.borderLeft =  "6px solid red";
         hypeDocument.getElementById("check4").innerHTML = "Check Again!"; 
         
 });	
 
 
 score5.focus();

I’ve tried to incorporate your suggestion as in below but it does not return any results.

  var vStepAnswer = scoreData.staves[0].measures[0].noteSets[0].notes[0].step.join("");
	var wStepAnswer = scoreData.staves[1].measures[0].noteSets[0].notes[0].step.join("");
	var xStepAnswer = scoreData.staves[2].measures[0].noteSets[0].notes[0].step.join("");


	var stepKeysAnswers  = [['vAnswer',vStepAnswer,"E"],['wAnswer',wStepAnswer,"F"],['xAnswer',xStepAnswer,"D"]]// we put the feed back answers in arrays with the correct answer key name.


   var  correctAnswers = {vAnswer:["135"],wAnswer:["135"],xAnswer:[ "135"] } //-- all correct answers

Yep, That completely wrong :confused:

From the looks of it each feed back i.e,

var vStepAnswer = scoreData.staves[0].measures[0].noteSets[0].notes[0].step
--> 6

will only return a single digit not an array. So you cannot sort it nor join its objects because it is a single item.

So I am a little confused as to what you are trying to do here.

I can guess again but I think it would help if you posted the results you are actually getting back and then how you need to put them together.