# Basic JavaScript Day 3: Homework

**URL:** <https://group.classcentral.com/t/basic-javascript-day-3-homework/35089>\
**Category:** JavaScript Bootcamp\
**Created:** [September 7, 2022, 8:53am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089 "2022-09-07T08:53:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hola\_soy\_milk](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/hola_soy_milk/32/600_2.png) [@hola\_soy\_milk](https://group.classcentral.com/u/hola_soy_milk)\
**Post date:** [September 7, 2022, 8:53am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/1 "2022-09-07T08:53:53Z")

</div>

@here

Hey JavaScripters!

Congratulations on making it through week 1 of the free JavaScript bootcamp.

📢 **Quick reminder about homework for the next session!**  
On Monday we’ll start with the exercise _ **Build JavaScript Objects** _ , so your homework is to do the exercises until that point:

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects)**
>
> Learn to Code — For Free

Remember folks, you’re not alone.  
Don’t hesitate to seek help in the #js-help channel or on the forum!

Be sure to collaborate, be sure to ask lots of questions, and be kind to one another!

Finally, to quote the great Stevan on Discord: ✨ Remember its a marathon not a sprint. ✨

Not every single concept will make sense right away, and **that’s okay**. The important part is to be exposed to these and practice so that they will click eventually.

We’re all in this together!  
Happy Coding!  
#PatPat 🤚

---

<div class="post-metadata">

**Author:** ![markspencer2928234](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@markspencer2928234](https://group.classcentral.com/u/markspencer2928234)\
**Post date:** [September 7, 2022, 2:02pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/2 "2022-09-07T14:02:58Z")

</div>

Thank you, that was a tough one !! Home work done

---

<div class="post-metadata">

**Author:** ![adammassey2962685](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@adammassey2962685](https://group.classcentral.com/u/adammassey2962685)\
**Post date:** [September 7, 2022, 9:04pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/3 "2022-09-07T21:04:44Z")

</div>

Hey ok i think im way of here but ive looked at it for too long and the words dont even look like words anymore can someone help with the golf code :

Heres what i have 🙂  
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,par){  
return “Hole-in-one!”;  
} else if (strokes \<= par -2){  
return “Eagle”;  
} else if (strokes = par-1){  
return “Birdie”;  
} else if (strokes = par){  
return “Par”;  
} else if (strokes = par + 1){  
return “Bogey”;  
} else if (strokes = par + 2){  
return “Double Bogey”;  
} else {  
return “Go Home!”  
}  
}

// Only change code above this line

golfScore(1, 1);  
golfScore(4, 2);

---

<div class="post-metadata">

**Author:** ![adammassey2962685](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@adammassey2962685](https://group.classcentral.com/u/adammassey2962685)\
**Post date:** [September 7, 2022, 9:05pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/4 "2022-09-07T21:05:34Z")

</div>

This bit appears to be where im going wrong if (strokes,par){ but ive tried many variations and cant think of an idea

---

<div class="post-metadata">

**Author:** ![hola\_soy\_milk](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/hola_soy_milk/32/600_2.png) [@hola\_soy\_milk](https://group.classcentral.com/u/hola_soy_milk)\
**Post date:** [September 7, 2022, 9:09pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/5 "2022-09-07T21:09:56Z")

</div>

That’s a single equals sign I see. are you comparing? You should be using double equals

---

<div class="post-metadata">

**Author:** ![sweet\_candy](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@sweet\_candy](https://group.classcentral.com/u/sweet_candy)\
**Post date:** [September 7, 2022, 9:21pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/6 "2022-09-07T21:21:27Z")

</div>

Thank you, I am done with the assignment #patpat

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 8, 2022, 12:18am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/7 "2022-09-08T00:18:54Z")

</div>

Hey, this one may feel a little like a trick question, but for the ‘hole in one’, the stroke IS one (and par does not matter). Does that help with fixing up your condition?

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [September 8, 2022, 3:54pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/8 "2022-09-08T15:54:15Z")

</div>

Hi Adam,

Surely for hole in one you need to be looking at strokes == something and par is irrelevant. Hope this helps.

Cheers

Bill

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [September 8, 2022, 4:17pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/9 "2022-09-08T16:17:03Z")

</div>

Hi all, really struggling with the return early from functions. I am lost as to what to put between the two comment lines. I have been working along the lines of

if (a\<0) {  
break;  
}  
if(b\<0){  
break  
}  
But I can’t get it to give me undefined - I can get NaN but not undefined

Any help would be appreciated on this one

Cheers all, hope it’s going well for you

Bill

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [September 8, 2022, 4:42pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/10 "2022-09-08T16:42:42Z")

</div>

Hi all,

Has anyone else noticed that if you left arrows on the homework’s it takes your tick of the one you go to so you lose the record that you have done it! Don’t go back folks!

Cheers

Bill

---

<div class="post-metadata">

**Author:** ![EnzoSnyman](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/enzosnyman/32/6331_2.png) [@EnzoSnyman](https://group.classcentral.com/u/EnzoSnyman)\
**Post date:** [September 8, 2022, 6:02pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/11 "2022-09-08T18:02:08Z")

</div>

some of these assignments got me scratching my head and when I finally figured it out I think to myself “but why?”  
75/113 completed. Will catch up the remaining 8 tomorrow

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 9, 2022, 3:19am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/12 "2022-09-09T03:19:17Z")

</div>

Hey! So, a _break_ is used typically in a loop (or switch) statement to ‘stop whatever you doing’ and step out of the loop/switch itself. To _return_ from a function early, you would use **return**. The return statement can be used to give back a value (int, float, string, boolean, other functions etc) BUT you can also return nothing, in which case it will be _undefined_. Also if a function completes but does not return anything explicitly, it **will** return undefined. An example:

```auto
function test(a,b){
  if (a < b){
    return; // undefined
  }
  if (a === b){
    return; // undefined
  }
  // even if we end up here.. the function will return: undefined 
}

console.log(test(3,4))
console.log(test(5,5))
console.log(test(5,4))

// output:
// undefined
// undefined
// undefined

```

I hope this helps?

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [September 9, 2022, 5:18am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/13 "2022-09-09T05:18:45Z")

</div>

Hi Syl,

Thank you so much for this explanation. This was the information that I really needed to get past this point, I wasn’t aware that a return with nothing to return would result in undefined. I completed the exercise with no problem.

I love that we can collaborate and help each other so easily on this platform, without giving the explicit code answer away.

Thanks again,

Bill

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [September 9, 2022, 7:54am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/14 "2022-09-09T07:54:49Z")

</div>

Interesting, I have run this now with both if statements and a switch statement but I can’t get the switch statement to work

switch(abTest) {  
#later edit - this is my problem, I have put the name of my function instead of the variables! I made the same mistake in card counting but have now learned something important!  
case(a \< 0):  
return;  
case(b \< 0):  
return;  
}  
.  
.  
.  
console.log(abTest(-2,2))  
NaN

I really don’t think that is the way it should work, but that is what it does for me!

My understanding was that the switch statement I have written is equivalent to:

if (a \< 0) {  
return;  
}  
if (b \< 0) {  
return;  
}  
but apparently not, because this works and the switch doesn’t - not at all clear what is wrong with the switch code.

Cheers

Bill

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 9, 2022, 12:22pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/15 "2022-09-09T12:22:13Z")

</div>

Hi Bill,

The switch statement evaluates an expression, and matches its value in the following case clauses. If it finds a match, it would execute that code block, until it encounters a break (or reaches the end of the switch statement). So you would need to use like this:

```auto
const quality = 10; 
// suppose we would judge 1-3 as poor, 4-5 as low, 6-7 medium, 8-10 good
// we can now evaluate 'quality' and give our opinion
// because multiple ratings can be grouped, we can make use of the 'fall through' principle
// this runs the code in the block from the case that is true, until it hits a break
switch (quality) {
  case 1:
  case 2:
  case 3:
    console.log('POOR');
    // you can run more code here .. and it will execute where quality is 1,2,3 and until it hits 'break'
    break; // here it breaks out of the switch if quality is 1,2,3
  case 4:
  case 5:
    console.log('LOW');
    break;
  case 6:
  case 7:
    console.log('MEDIUM');
    break;
  case 8:
  case 9:
  case 10:
    console.log('GOOD'); 
    break;
  default:
    console.log('Quality UNKNOWN');
}

```

Now if you go back to your card counting problem, you can see that part of the puzzle is to return a value of -1, 0, 1 for certain types. That screams for a switch statement.

The switch takes an expression, so it could also be something like (a\<b) and then cases true and false (although I would suggest to do this with an if 🙂 )

ALSO: from your code example it looks like you try to ‘call’ your switch statement? that is not possible. The switch will be executed where it is encountered. So it can be part of a function that can be called.

```auto
function myFunction(quality){

    // ...

    switch(quality){
        // ...
    }

    // ...

}

// call the function
myFunction(5);

```

and when you add code to your posts, if you wrap it between 3 backticks, it formats like code ``` … ```

I hope this helps?

Cheers,  
Syl

---

<div class="post-metadata">

**Author:** ![nems](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/nems/32/6306_2.png) [@nems](https://group.classcentral.com/u/nems)\
**Post date:** [September 9, 2022, 10:31pm UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/16 "2022-09-09T22:31:08Z")

</div>

Basic JavaScript Day 3: Homework Done

 ![Screenshot from 2022-09-10 01-14-56](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/2X/e/e3bcb71af3f15deffec09c736901b4b9831da17b.png)

---

<div class="post-metadata">

**Author:** ![RiverXeno](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/riverxeno/32/6307_2.png) [@RiverXeno](https://group.classcentral.com/u/RiverXeno)\
**Post date:** [September 10, 2022, 2:22am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/17 "2022-09-10T02:22:21Z")

</div>

Just finished catching up on all the lessons so far. Thanks for the live lessons, loving this bootcamp, it is really helpful! 🥳

---

<div class="post-metadata">

**Author:** ![benlee2922930](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/benlee2922930/32/5196_2.png) [@benlee2922930](https://group.classcentral.com/u/benlee2922930)\
**Post date:** [September 10, 2022, 2:42am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/18 "2022-09-10T02:42:40Z")

</div>

Hi Everyone,  
I’m super confused. 😕  
Can anyone tell me why in this construction of the answer below, they are using “strokes” as variable in the if statement? I thought that you had to pass the function “par” first then “stokes” as in (par,stokes) below. I thought you’d need to construct it like so:  
if golfScore(4,1) {  
return “Hole-in-one!”  
}

“not my work below”  
function golfScore(par, strokes) {  
// Only change code below this line  
if (strokes == 1) {  
return names[0];  
} else if (strokes \<= par - 2) {  
return names[1];

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 10, 2022, 2:47am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/19 "2022-09-10T02:47:02Z")

</div>

Hi Ben,

To be honest, I had to think long and hard too on this question but once I figured out how par and strokes relate it all made sense. The first condition is a bit of a trick one, and the name gives it away: “Hole-in-one!”. Par does not play a role here, it only needs strokes and you ONLY get a hole in one if it took you 1 stroke (stroke==1). For all the other conditions you have to test if the number of strokes meets a combination of par and the deviation.

The condition to test uses the input arguments for **par** and **strokes**. You do need to build those conditions yourself based on the instruction and test against them with if / else if / else statements. Remember that you can have more than one else if.

Also the code asks you to return the correct phrase when a condition is met. Remember you can step out of the function by using return and then the value. For that you can use the value in the array ‘names’ that is given at the top of the code. The structure of your code would be something like this:

```auto
function golfScore(par, strokes) {

  if (condition){
    return names[index];
  } else if (condition) {
    return names[index];
  } else {
    return names[index];
  }

} 

```

Does this help?

I’ve set up a small visualization how the code runs [here](https://pythontutor.com/render.html#code=const%20names%20%3D%20%5B%22Hole-in-one!%22,%20%22Eagle%22,%20%22Birdie%22,%20%22Par%22,%20%22Bogey%22,%20%22Double%20Bogey%22,%20%22Go%20Home!%22%5D%3B%0A%0Afunction%20golfScore%28par,%20strokes%29%20%7B%0A%20%20//%20Only%20change%20code%20below%20this%20line%0A%20%20if%20%28strokes%20%3D%3D%201%29%7B%0A%20%20%20%20return%20names%5B0%5D%3B%0A%20%20%7D%20else%20if%20%28strokes%20%3C%3D%20par%20-%202%29%20%7B%0A%20%20%20%20return%20names%5B1%5D%3B%0A%20%20%7D%20else%20if%20%28strokes%20%3D%3D%20par%20-%201%29%20%7B%0A%20%20%20%20return%20names%5B2%5D%3B%0A%20%20%7D%20else%20if%20%28strokes%20%3D%3D%20par%20%29%20%7B%0A%20%20%20%20return%20names%5B3%5D%3B%0A%20%20%7D%20else%20if%20%28strokes%20%3D%3D%20par%20%2B%201%29%20%7B%0A%20%20%20%20return%20names%5B4%5D%3B%0A%20%20%7D%20else%20if%20%28strokes%20%3D%3D%20par%20%2B%202%20%29%20%7B%0A%20%20%20%20return%20names%5B5%5D%3B%20%20%20%20%0A%20%20%7D%20else%20%7B%0A%20%20%20%20//%20in%20all%20other%20cases...%0A%20%20%20%20return%20names%5B6%5D%3B%0A%20%20%7D%0A%0A%20%20return%20%22Change%20Me%22%3B%0A%20%20//%20Only%20change%20code%20above%20this%20line%0A%7D%0A%0AgolfScore%285,%209%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D&textReferences=false) . You can step through the code and see how each condition is evaluated before it decides what to return.

---

<div class="post-metadata">

**Author:** ![robiul121](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/robiul121/32/1935_2.png) [@robiul121](https://group.classcentral.com/u/robiul121)\
**Post date:** [September 10, 2022, 5:51am UTC](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089/20 "2022-09-10T05:51:54Z")

</div>

![2022-09-10 11 50 38](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/2X/b/b12f317b6cac04085a364e720b4cbe888d162dc3.png)

- 

> Homework-3 Done!

-

[Next page](https://group.classcentral.com/t/basic-javascript-day-3-homework/35089.md?page=2)
