Basic JavaScript Day 3: Homework

@here

Hey JavaScripters!

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

:loudspeaker: 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:

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: :sparkles: Remember its a marathon not a sprint. :sparkles:

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 :raised_back_of_hand:

12 Likes

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

2 Likes

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 :slight_smile:
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);

4 Likes

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

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

6 Likes

Thank you, I am done with the assignment #patpat

3 Likes

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?

1 Like

Hi Adam,

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

Cheers

Bill

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

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

1 Like

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

2 Likes

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:

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?

3 Likes

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

2 Likes

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

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:

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 :slight_smile: )

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.

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

3 Likes

Basic JavaScript Day 3: Homework Done

2 Likes

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

3 Likes

Hi Everyone,
I’m super confused. :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];

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:

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 . You can step through the code and see how each condition is evaluated before it decides what to return.

1 Like

Homework-3 Done!

3 Likes