Basic JavaScript Day 3: Homework

Hi Ben,
Did your approach run and for the second approach using only "strokes argument) how does it handle the test for golfScore(5,5) and golfScore(4,5) which returns 2 different strings?

1 Like

Hello Syllie

Thanks so much for your reply. If I amy, I have a few more questions.

I understand the logic of if, else if, and else, but I’m not sure about how the conditionals are constructed. I see that you’re using par as a variable, but I don’t see where par is defined. In your example “(strokes <= par - 2)” , is par equal to four?

I’m I understanding (strokes <= par - 2) correctly to means strokes is less than or equal to two*? If that is true, would less than two, suggest that it could be one?

Am I correct in that you do not have to pass a value into all variables within a function? As in this example, it is only necessary to pass/define the strokes variable?

Thank you!

Ben

1 Like

Hey Ben,

That is a good question and it actually has to do with how functions work. Not sure if you are in the discord channel but there was a similar question this morning that was explained well.

In short: a function can accept one or more parameters, and they are passed in when calling the function with arguments.

So in our example the function has a signature like this:

function myFunction(param1, param2) {
    // Do something ... with param1 and param2
}

and then it is called like this:

myFunction(a, 6);

In my simple example it does not matter what a and 6 are, but you should know that by calling myFunction like that, a will be used as param1 and 6 will be used as param2.

Or like this

myFunction(b, 10)

Now param1 will get the value b, and param2 will get the value 10.

In the function itself the code would be use param1 and param2 to define what needs to be done with these parameters (conditions, returns, calculations, calling other functions etc).

In some code you will see things like

function readMe(text){   // this 'text' is LOCAL and ONLY known in this function. It could have been txt too
    console.log(text)  // of course you would use the same variable here, so if using txt in the definition, you should use it here too!
}

text = "bla bla bla";  // this text is in a different scope and not the same variable as used in the readMe function signature
readMe(text); // but here we give that 'different' text to the function

Personally I find that a bit confusing as they have ‘different scope’ and I would use different variable names (like text and txt) but there are many programmers that keep them the same. Understanding scope is very important.

Oh and to actually answer your question… in the golf exercise, the function definition takes in values for par (first parameter) and strokes (second parameter)

function golfScore(par, strokes) { 

   // now you can use par and strokes in your code without actually knowing what their values are
   // such as checking if the number of strokes is smaller than par -1 and then do something with it
   // the actual values are substituted when you *call* the function
   // and you DO need to have a value for both strokes and par here.
   if (strokes <= par - 1) {
        // do something
   }

}

Does this help?

2 Likes

I am right there with you. Sometimes I complete the task and still dont really understand it lol

2 Likes

Just keep at it Samual! Learning your first coding language is tough but it will ‘click’ in some time. Practice is key! And after that… learning any new programming language will come easy.

1 Like

Sellie l agree with you here. The more you practice the more the concept becomes clear.

2 Likes

Syllie ,

Thank you for helping me with this! With each reply I understand a bit better.
I know that there is always more than one way to solve something and it’s fascinating to see how different programs approach the problem. I like using the array with index method. I also found Ramon’s explanation [here] (Discord) very helpful as well.

I see , now, that I don’t have to define par as anything in order to use it in the conditional. Would you in theory have to define par at some point in order for the function to work? I’m not sure.

Have a good day.

1 Like

Not sure I understand your question. I didn’t use that construction (x,x) but rather (strokes <= par - 2).

Thanks syllie! I will. I really enjoy the challenge of this, even though I find myself banging my head against the wall constantly.

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/logical-order-in-if-else-statements

Okay Im a bit confused here. I dont think im grasping the concept so its hard for me to even ask the question. No matter what order I put these in its not working.

Thanks for your response.

I was curious to see how the implementation of:

golfScore(4,1) {
return “Hole-in-one!”
}

was done and if it passed all the 11 test cases. However,you’ve indicated that you didn’t use it.

Thanks and happy coding!.

1 Like

@hola_soy_milk Hey Ramón! Is it possible to review some of the day 3 homework? I finished it but still feel pretty lost.

1 Like

I’ll happily try to remember to review it during q and a! If I forget please call me out if you’re online!

Hey Ben,

It sounds like you need a little bit more help still? So this will be a bit of a spoiler, but I will not fill out all conditions so there will still be a bit of work to do.

Lets first analyze the problem. It gives us this table:

Strokes	    Return
1           "Hole-in-one!"
<= par - 2	"Eagle"
par - 1	    "Birdie"
par	        "Par"
par + 1	    "Bogey"
par + 2	    "Double Bogey"
>= par + 3	"Go Home!"

On the left we see exactly what conditions are expected and we can write this out in plain English:

  • if strokes is 1 then return “Hole-in-one!”
  • if strokes is less than par minus 2 then return “Eagle!”
  • if strokes is exactly par minus 1 then return “Birdie”

etc

From this you can see that you need BOTH strokes and par to solve the exercise.

Now lets have a look again at the starter code:

const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];

function golfScore(par, strokes) {
  // Only change code below this line


  return "Change Me";
  // Only change code above this line
}

golfScore(5, 4);

The course has provided the function signature, reading function golfScore(par, strokes) { ... }
So when this function is called on the last line with golfScore(5, 4); it gives the argument 5 for the parameter par (par = 5) and the argument 4 for the parameter strokes (strokes=4).

So this function will run with this call having values for par (5) AND strokes (4).

You do not define the value, the value is given in when you call the function.

So in your function you would need to set up tests, based upon the conditions as given in the table, and that would use an if / else if / else type construction.

Spoiler for the first bit:

const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];

function golfScore(par, strokes) {
  // Only change code below this line
  if (strokes == 1) {  // we called the function with strokes 4, so not true, we move to the next condition
    return names[0];  // this will return the first element from the names array, being "Hole-in-one!"
  } else if (strokes <= par - 2) {  // we called the function with strokes 4 and par 5. The condition 5 <= (4-2) is not true, we move to the next condition
    return names[1]; // "Eagle"  
  }
  // now write here all the other conditions.. till you reach the last one.. where you can just do 'else' as you have now tested everything. The one you need get your correct response is also in here.
  else {
    return names[6]; // "Go Home!"
  }
  return "Change Me";
  // Only change code above this line
}

golfScore(5, 4);

The tests to see if your function is correct will call it with other values too, and you need to pass them all!
The tests that are run are shown in the bottom left of your screen, under 'Tests" such as
golfScore(4, 1) // should return the string Hole-in-one!

So you write a function once and call it many times with different values. In calling the function, you provide the values for par and strokes as arguments (you give them as input values). The function then executes and does something with those values for par and stroke.

I think this is the best I can explain it, and I hope it helps.

1 Like

Hi Ramon,

From both discord and various posts in this thread, and also this one https://group.classcentral.com/t/basic-javascript-day-1-homework/34603/42 it looks like there is still a lot of struggle understanding the difference between a function definition and a function call. Other obvious struggles are with how the if / else if / else works. And a lot of people seem to have missed the array at top of screen defining return values. I think it would benefit a lot of people to work through the golf code exercise on the stream.

2 Likes

Thank you, I wont be online because thats like 2am my time :grinning:

totally Syl! im just not grasping most of the concepts you mentioned completely.

Thank you so much, Syl!

I’ll cover it during Q&A

1 Like

Day 3 Homework Done!

1 Like

Hi Syllie,

thanks for the explanation, but I thought that par and strokes had to always be numeric and positive? Or does this apply only when they are being called and not in ‘else if (strokes <= par - 2)’?

That part confused me in coding it like this:
else if (par = 5, strokes = 7) {
return names[5];