I'm using MyBigFunction to do two tasks:
1. Find a row in MyArray by matching the first argument of MyBigFunction ("Name") to the property FirstName.
2. Finding the X:Y property with the same X-number as the second argument of MyBigFunction (QuestionNumber) and returning the Y value.
So, when I ask MyBigFunction("Claire", 1) it finds the row named Claire and returns the answer stored in QuestionNumber 1: 600. This works fine.
var FindIndex
var MyArray = [
{FirstName:"Aaron", 1:12, 2:30, 3:43},
{FirstName:"Bob", 1:60, 2:90, 3:44},
{FirstName:"Claire", 1:600, 2:12, 3:13}
]
function MyBigFunction(Name, QuestionNumber) {
var FindIndex = MyArray.findIndex(k => k.FirstName == Name);
return MyArray[FindIndex][QuestionNumber];
}
console.log (MyBigFunction("Claire", 1)); // returns 600, as expected
However, now I want to change some of the answers in MyArray to functions. For example, I changed Claire's Question 3 to a function called PopQuiz, which checks if the argument "Surprise" passed to it is greater or less than 100 (returning 90 if greater, or 80 if less than).
And, I've added a third argument, Surprise, to MyBigFunction. So when I ask MyBigFunction("Claire", 3, 40), it should look up the line "Claire," look at question 3, realize it's a function, give it the value 40, and let PopQuiz evaluate that 40<100, function, returning 80. But instead I get "undefined."
var FindIndex
var MyArray = [
{FirstName:"Aaron", 1:12, 2:30, 3:43},
{FirstName:"Bob", 1:60, 2:90, 3:44},
{FirstName:"Claire", 1:600, 2:12, 3:PopQuiz(Surprise)}
]
function MyBigFunction(Name, QuestionNumber, Surprise) {
var FindIndex = MyArray.findIndex(k => k.FirstName == Name);
return MyArray[FindIndex][QuestionNumber];
}
function PopQuiz(Surprise) {
if (Surprise >= 100) {return 90;}
else if (Surprise < 100) {return 80;}
}
console.log (MyBigFunction("Claire", 3, 40)); // should return 80, but returns undefined
So, what am I doing wrong here? Is it impossible to get MyBigFunction to give values to PopQuiz?
Thanks in advance for your insight.