why are you getting this error. to give you a very simple answer is because you are mixing string variables with number variables, in my case i have a loop using for (in) loop and when i do the loop, the property i was using is automatically a string because i was getting it from an object from my API. for example
my object = {"895" : "This is eight hundred and ninty five"}
as you can see from my object, 895 is not a number but a string and i was using a IF conditional operator to match a pair of variables, this is where my code was getting the error:
if(QuestionId !== lastQuestionId && lastQuestionId!== 0){
the variable QuestionId
was a string type and lastQuestionId
was a number (interger) type so thats why i was getting the error, to fix it i just changed it to:
if(QuestionId !== lastQuestionId && lastQuestionId!==''){
the code with the error was like this:
for(let QuestionId in this.factoryQuestion) {
let wrongAnswersFound:boolean = false;
//alert(QuestionId); // 1546
// NOW LOOP TRHOUG QUESION OPTIONS
let lastQuestionId:number=0; // this is to keep track of the currecnt QuestionId in the loop below so it does not repeat the same QuestionId
// do this loop to only one QuestionId
for(let AnswerId in this.factoryQuestion[QuestionId].options) {
if(QuestionId !== lastQuestionId && lastQuestionId!== 0){
//alert(AnswerId); 2646
if(this.factoryQuestion[QuestionId].options[AnswerId].answeredWrong){
// alert('wrong');
wrongAnswersFound = true;
this.Final.answeredWrong.push(QuestionId); // add QuestionId to the answeredWrong array
lastQuestionId = QuestionId; //ignores the resto of the wrong answers for the same questionId
}
}
}
AND THE FIXED CODE LOOKS LIKE THIS:
for(let QuestionId in this.factoryQuestion) {
let wrongAnswersFound:boolean = false;
//alert(QuestionId); // 1546
// NOW LOOP TRHOUG QUESION OPTIONS
let lastQuestionId:string=''; // this is to keep track of the currecnt QuestionId in the loop below so it does not repeat the same QuestionId
// do this loop to only one QuestionId
for(let AnswerId in this.factoryQuestion[QuestionId].options) {
if(QuestionId !== lastQuestionId && lastQuestionId!==''){
//alert(AnswerId); 2646
if(this.factoryQuestion[QuestionId].options[AnswerId].answeredWrong){
// alert('wrong');
wrongAnswersFound = true;
this.Final.answeredWrong.push(QuestionId); // add QuestionId to the answeredWrong array
lastQuestionId = QuestionId; //ignores the resto of the wrong answers for the same questionId
}
}
}
Notice the difference? (in bold)