Defining variables within an if/else block

Writing the validation code that way does work. ( I ) couldn’t validate first before calculating the average…thx

1 Like

Here’s my take on the question, i will give you the breakdown without giving you the answer. It is so important you code this yourself, do not just copy and paste other peoples code - you will not learn anything !

Step 1 - create a function that takes 3 grades as input - think about what the () are for in functions, they take parameters. You can call these parameters whatever you wish, it does not matter.
Step 2 - Check that the 3 grades are in range (more than 0 but less than 100) - think about operators, in particular the more than, less than and OR operator.
Step 3 - calculate the average of the 3 grades - when doing this remember PEDMAS ! add the 3 grades together and divide by 3 to get the average. Assign this average to a variable.
Step 4 - Run an if/else block, or a switch block if you prefer to, checking the average variable against the stated grades on this exercise. Again think back to our operators, more than, less than. The objective here is to compare the average score against a set of conditions.
Step 5 - To check your code is working run a few tests. Remember to use console.log to output your results to the screen. Also don’t forget where the values are placed (inside the brackets) ! ()

I hope this is helpful to the new starters out there. Happy to help

Extra Study

We see how cumbersome the OR expression can be, even with just three values. That’s six operands. What happens when there are other than three values?

JavaScript gives us the rest parameter as a tool to deal with this variability in the number of arguments.

const finalGrade = (...y) => {
  const fit = x => x >= 0 && 100 >= x;
  const sum = (a, b) => a + b;
  const index = x => x < 6 ? 0 : x > 9 ? 4 : x - 5;
  for (let x of y) {
    if (! fit(x)) return "You have entered an invalid grade."
  }
  return 'FDCBA'[index(Math.floor(y.reduce(sum) / y.length / 10))]
}
console.log(finalGrade(50, 60, 70, 80, 90, 100, 10))      //  D
console.log(finalGrade(50, 60, 70, 80, 90, 100, 10, 10))  //  F
console.log(finalGrade(100, 100, 100))                    //  A
console.log(finalGrade(100, 95, 90))                      //  A
console.log(finalGrade(80, 85, 90, 84))                   //  B

Notice how the rest parameter turns y into an array? It is why we can implement the reduce method on it, and it shows up as iterable when we apply of to it in the for loop, or even measure its length.

Rest parameters were introduced in order to give arrow functions an arguments list. ES5 functions have an arguments object, along with this and golly gee what else that arrow functions, stripped of their armor don’t have. They are meant to be simple, concise and pure. With the same power in distinguishing a variable arguments list, the simple function retains its horsepower.

Helper functions are nothing new. How they fit into the arrow function picture is seamless. There are no constraints. Better still if they are concise body functions, meaning very lightweight.

Looping over the arguments list, we snag the first invalid argument and shoo it home. Simple, yet adaptable to any sample space. This as opposed to using logical expressions. While we do use one above, it is abstracted away into a single purpose, pure, helper function.

It is not always logic, but an open door that leads us in the direction of another solution. Bottom line for this extra study session is, go through the door. Explore, study, discover, postulate, experiment, learn. It’s down these paths that the juices get flowing. Starting out they are tempting but we should avoid them. Bookmarks are there for a reason. This will keep. In time it will be you writing it, not me.

Be your intuitive self.