Jump to content

How do I pass function's argument to a second function INSIDE an array?


NL268

Recommended Posts

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.

Link to comment
Share on other sites

two things:

 

  1. your MyArray definition is setting the function PopQuiz(Surprise) as the value for 3:, but you're trying to use Surprise there, which is not defined anywhere, thus your error.
  2. Look into using Javascript objects more. You are doing some complicated stuff that could be made easier using JavaScript objects.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...