Basic JavaScript Day 3: Homework

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

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.