How does this function know what the value of cups is?

Tell us what’s happening:

this answer uses a loop to count how many ‘cups’ to make, but I can’t see where the value of cups is given to the value of teaCup.

Your code so far
Everything I tried is a mess. I tried to use the cup value defined inside the getTea function - but it’s a mess.


// Function that returns a string representing a cup of green tea
const prepareTea = () => 'greenTea';

/*
Given a function (representing the tea type) and number of cups needed, the
following function returns an array of strings (each representing a cup of
a specific type of tea).
*/
const getTea = (numOfCups) => {
const teaCups = [];

for(let cups = 1; cups <= numOfCups; cups += 1) {
  const teaCup = prepareTea();
  teaCups.push(teaCup);
}
return teaCups;
};

// Only change code below this line
const tea4TeamFCC => {
getTea(40);
};
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Learn About Functional Programming

Link to the challenge:

I feel like you are overcomplicating this. The instructions say

In the code editor, the prepareTea and getTea functions are already defined for you. Call the getTea function to get 40 cups of tea for the team, and store them in the tea4TeamFCC variable.

You need to call the function. That’s it.

1 Like

Hmmm.

I’m trying to understand how the code provided works. It’s unclear how the value of cups is available to the rest of the code.

It’s in the function argument numOfCups.

How does it get there?

You make function call and pass in a value, like with any other function call.

Does one of the earlier lessons in this course explain how that works?

I can’t recognise the principles involved in how the value of cups is communicated to the value of numOfCups. I clearly need to go back to an earlier stage and try to learn that principle before I can get through this lesson. Thank you for trying to help me.

myFunction(myArugment)

You use function call syntax here like any other time you’ve made a function call.

@mmmmm, I understand your focus is how the code is working, not the test only.
Here I’m trying to make it clear if you can understand, hope this will help you…

Step1:

// making a function, we'll use it 

function getTea(howManyCups){
  // we'll use howManyCups to make this function working
  console.log(howManyCups);
  return howManyCups;
  }
  
  // calling getTea function without any value
  // it prints nothing/ undefined
  getTea();  
  // calling getTea function with a value of howManyCups
  getTea(1) // this will print 1
  // calling the same function with a different value
  getTea(5) // this will print 5
  //calling the same function with another value
  getTea(40) // this will print 40
  
  // now to understand a for loop
  // i'm just trying to limit my explaination here
  // but hope enough to understand for you
  // as you appeared this point of challenge

Step 2:

//think this code in for loop
function getYourTea(howManyCups){
  // we'll use howManyCups to make this function working
  
  var teaStore =[]; // we'll use it later, this will store ready tea
  var tea=  "person:give me a cup of tea, maker:hey your tea is ready";

  
  for(let i=1; i<=howManyCups; i++){
  // this will make a checkpoint with a value of i
  // and here value of i is a range between 1 to howManyCups
  // it will count first i=1; then i will change to i++
  // means it will be ready for nex count  1+1 or 2 
  // for the next operation, then 2+1, then 3+1 ...
  // once it will meet the last condition i<=howManyCups
  // it will stop checking more vale and then stop
  // this will continue every time 
  // until it ends upto the proces means howManyCups set
  // so if you call the function with a value of 10
  // ten times it will work as we order
  
  // so far, now if you want to send the ready tea each time to serve
  // now the time to send tea to the serve counter
  
  teaStore.push(tea);
  // so if you call your function 10 time 
  // your teaStore will receive 10 times
  // "person:give me a cup of tea, maker:hey your tea is ready"
  }
  
  // as our all tea is ready
  // deliver all you have in teaStore to the persons 
  return teaStore;
  
  }
  
  // now if you call your work function for 10 cups
  getYourTea(10);// it will make and deliver 10 cups tea

This all for your understanding the process. :grinning:

Thank you.

Thank you for trying to help me.

The part that is confusing me is still a mystery.

In your example, the for loop uses a variable called howManyCups, and that same variable is used in the getYourTea function. It makes sense to me that given that setup, getYourTea would know the number. I don’t understand how your teaStore function knows the value of howManyCups.

I’m trying to understand how the teaStore (in your example) knows that the value of tea is the equivalent of the value of howManyCups.

I dont recognise this syntax. I’ve been back through about 10 of the lessons in earlier chapters to try to find where that structure is explained, but have not yet found it. If you know the name of the principle, it might help me locate the lesson that explains the concept. Clearly I’ve missed the point in the earlier stage.

Thank you.

Sorry - I can see it now.

I don’t know how that took me so long.

cups is defined by reference to the increment on numOfCups, which is set by the value passed into getTea.

Thank you.