0

I was doing some javascript exercises and although I did manage to make this one correctly, I'm pretty confused.

So this is an exercise where the average of a list of marks has to be compared with a given value range to return a string that responds whether the mark was sufficient or not. I found that when compare using a > operator, it returns undefined. Even if the upper limit is also given and that alone (without the lower border value + > operator) does return the string. I don't really understand why this is.

Thanks for your help

const marks = [ 80, 80, 50 ]

console.log (calculateGrade (marks));

function calculateGrade(marks) {
    let markstotal=0;

    for (let mark of marks)
        markstotal += mark;
        let average = markstotal / marks.length;

         if (average <= 59)
        return "F";
         if (average < 59)
         return "mehh";
         if (average > 70 && average < 90)
         return "oh yeah mate";
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61

2 Answers2

0
const marks = [80,80,50]
function calculateGrade(marks){
    var markstotal = 0
    marks.forEach(mark => markstotal+=mark);

    console.log(markstotal)
    let average = markstotal/marks.length
    if (average <= 59)
        return "F"
    else if (average > 59 && average <70)
        return "meh"
    else
        return "Fuckyeahhmate";
}
console.log(calculateGrade(marks))

I suggest using .forEach method instead. if you want to iter through a loop is also possible

var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    console.log(myStringArray[i]);
    //Do something
}

Please paste the code next time!!

Travis Tay
  • 350
  • 2
  • 10
  • Why is the forEach method preferable? Is it quicker? – Justalamp Jun 10 '20 at 09:39
  • It's actually not quicker. Using it actually improves readability. More towards functional programming whereby at a glance we can tell what the code is doing. In my opinion, since calculating the average can be done in a simple one-liner why not. The link below explains it well. but its really down to personal preference. https://stackoverflow.com/questions/43031988/javascript-efficiency-for-vs-foreach – Travis Tay Jun 10 '20 at 16:48
0

You don't return something for all possible values of average. In your example the value of average will be 70, which does not meet any of the available if conditions in your code and so it reaches the end of the function without returning anything. The default return value is undefined.

Make sure your function always returns something. Lets try this instead.

const marks = [80, 80, 50]

console.log(calculateGrade(marks));

function calculateGrade(marks) {
    let markstotal = 0;

    for (let mark of marks)
        markstotal += mark;
    let average = markstotal / marks.length;

    if (average < 60)
        return "F";
    else if (average < 70)
        return "getting hot";
    else if (average < 90)
        return "oh yeah mate";
    else 
        return "you are a god!";
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • @Justalamp if any of the answers solve your problem you can upvote, and accept the answer by clicking the tick on the left :) – Paul Rooney Jun 11 '20 at 23:28