Basic JavaScript Day 3: Homework

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”


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 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.


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];

Hi Remco,

I think you misunderstand how the else if works. You cannot call that. It is simply a condition that is checked, and will have an outcome of either true or false. So, you can call a function, and in doing so pass some arguments in. The parameters of the function will assume the values you pass in and the condition is evaluated.

An example:

function myFunction(a,b) {
    if (a > b) {
        console.log('first check true');
    } else if (b > a) {
        console.log('second check true');
    } else {
        console.log('the input values are equal')  // only option left

So now when you call it like this myFunction(3,4) it will substitute a = 3 and b=4 and the condition a>b will yield false, so it will step into the next check and the condition b>a will yield true and print second check true to the console.

I hope that makes sense?

Hello. Could somebody help me figuring out my mistake? Please and thank you in advance.

Hi Lina,

You are attempting to use the increment/decrement operator, but you wrote it with a space between the name and operator. Try it again using these operators correctly, like so: count++ and count--. I did not review your full code so there may be other things you need to change, but this is definitely not working as intended.

Hi Syl,

That makes sense :slight_smile: Thanks you very much!

1 Like

Thank you Syl for helping me. I changed it already, however, the same mistakes keep happening. Do you have any other solution?

Thank you again.

Can somebody explain why this function only works in this order? I can’t see the logic :slight_smile:

Thanks in advance!

Hi! Why we have to use === in the Golf exsecise

Hi Lina,

I now looked closer at your code, and sorry for not pointing this out earlier, but your case returns a string regardless of the actual value of count. As the count variable lives in the global space, it will keep its value on each call to the function. So to solve this challenge successfully, you need to do this in 2 steps:

  1. Evaluate the card and add either +1, 0, -1 to the count variable
  2. Evaluate the count variable, and if it is positive, return the current count and Bet, otherwise return the current count and Hold

Some other tips:

  • note that you do not need to ‘add’ anything when the card is 7, 8 or 8 - so you could consider leaving these out of the ‘case’ evaluation, if:
  • you evaluate and return the solution outside of the switch (aka: use the switch to change count, return your function after the switch)
  • you can stop evaluating the card after you found its value as case - so it would be better to break; (but the last case evaluation does not need it as you are done anyways).

I hope this helps Lina. If you are still stuck - let me know and I will post a code sample.


pfew Remco, you got me good there! I looked at your code and thought omygosh did I miss anything here as I cannot remember us doing so many (and such specific) condition checks!

I see what you have done, and yes you got the code to pass - but not in the right way yet. From the code I can see you had difficulties passing the test cases, and to deal with that you wrote a complex piece of code that dealt with each condition separately ( I use an analogy like: think of frying a piece of bacon, you press down where it bubbles up, only to see another bubble appear in a different spot - or in dutch: ‘brandjesblussen’). This way we coders would need to write hundreds of lines to make a function work and it still would not do. You did too much work!

Now to your problem. What we had to solve was:

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined

It asks you to do 2 checks, and you get the value of a and b in the header. This is really as easy as

if ( (a <  0)  ||  (b < 0) ) {
   return;   // as returning from a function without a value automatically is 'undefined' . 

You could do this as a one liner too, but again I usually use bracket notation all the time, just so I can slip in extra code with ease and never get confused what is part of a block and what not.

I am on discord too if you want a bit of a broader discussion around this.