Dulanjali Senarathna
Dulanjali Senarathna

Reputation: 23

I want to check the grade of the students marks , based on average

What's wrong with this code? I tried get marks using array and pass the array in to function parameters and calculate the average in that function.

const marks = [100,100,80];
var summ = 0;

function calculateGrade(){
    
    for(let i=0; i<=marks.length;i++){
       
        summ = summ+marks[i];
        var avg = (summ/marks.length);  
    }
    
    if(avg<=59){
        console.log('F');
    }
    else if(avg>=60 && avg<=69){
        console.log('D');
    }
    else if(avg>=70 && avg<=79){
        console.log('C');
    }
    else if(avg>=80 && avg<=89){
        console.log('B');
    }
    else if(avg>=90 && avg<=100){
        console.log('A');
    }
}

console.log(calculateGrade(marks));

Upvotes: 2

Views: 5647

Answers (6)

Alex
Alex

Reputation: 126

const sum = marks.reduce((partialSum, a) => partialSum + a, 0);

Upvotes: 2

Syful Islam
Syful Islam

Reputation: 24

` calculateGrade(){
                    let marks   = [100,100,80];
                    let summ    = 0;
                    let avg     = 0;
    
                    for(let i = 0; i < marks.length; i++){
                        summ    = summ+marks[i];
                        avg     = (summ/marks.length);
                    }
                    
                    if(avg<=59){
                        console.log('F');
                    }
                    else if(avg>=60 && avg<=69){
                        console.log('D');
                    }
                    else if(avg>=70 && avg<=79){
                        console.log('C');
                    }
                    else if(avg>=80 && avg<=89){
                        console.log('B');
                    }
                    else if(avg>=90 && avg<=100){
                        console.log('A');
                    }
                }`

> array start from 0

   

Upvotes: 1

Bishan
Bishan

Reputation: 15740

There are couple of mistakes in your code.

1.

for(let i=0; i<=marks.length;i++)

marks.length is 3. Array index starting from 0.

const marks = [100,100,80];

index 0 is 100. 
index 1 is 100.
index 2 is 80.

When you add i<=marks.length, this is equals to i<=3.

= in here will run the loop extra circle and this will return NaN because there are only 3 elements in you array and array indexing is 0 based.

2.

for(let i=0; i<=marks.length;i++){
   
    summ = summ+marks[i];
    var avg = (summ/marks.length);  
}

avg is out of scope. you have defined avg inside the loop and trying to access it outside of the loop. Anything declared in the loop is scoped to that loop and are not available outside the loop.

3.

console.log(calculateGrade(marks));

Your calculateGrade() function is not accepting any parameters. So you can't pass any parameter into this function.

4.

console.log(calculateGrade(marks));

since calculateGrade() function is not returning any value, this will print nothing. So you don't need to call this inside a console.log();.

I have simplified your code as below.

const marksArr = [100, 100, 80];
calculateGrade(marksArr);

function calculateGrade(marks) {
  console.log('calling calculateGrade(marks)...');
  var avg = (marksArr.reduce(function(a, b) {
    return a + b;
  }, 0)) / marksArr.length;
  console.log('avg is', avg);

  if (avg <= 59) {
    console.log('Grade', 'F');
  } else if (avg >= 60 && avg <= 69) {
    console.log('Grade', 'D');
  } else if (avg >= 70 && avg <= 79) {
    console.log('Grade', 'C');
  } else if (avg >= 80 && avg <= 89) {
    console.log('Grade', 'B');
  } else if (avg >= 90 && avg <= 100) {
    console.log('Grade', 'A');
  }
}

Upvotes: 1

user2182349
user2182349

Reputation: 9782

You are very close

const marks = [100, 100, 80];


function calculateGrade(marks) {

  let summ = 0;

  for (let i = 0; i < marks.length; i++) {
    summ += marks[i];
  }
  const avg = summ / marks.length;
  let grade = '';
  if (avg < 59) {
    grade = 'F';
  } else if (avg <= 69) {
    grade = 'D';
  } else if (avg <= 79) {
    grade = 'C';
  } else if (avg <= 89) {
    grade = 'B';
  } else {
    grade = 'A';
  }
  return grade;
}

console.log(calculateGrade(marks));

Upvotes: 1

Javaid
Javaid

Reputation: 375

 const marks = [100, 100, 80];
  var summ = 0;
   
  //issue one (Tmarks were missing )
  function calculateGrade(Tmarks) {
    // issues 2 ( <= should be < )
    for (let i = 0; i < Tmarks.length; i++) {
      summ += Tmarks[i];
    }
    var avg = summ / Tmarks.length;
    if (avg <= 59) {
      console.log("F");
    } else if (avg >= 60 && avg <= 69) {
      console.log("D");
    } else if (avg >= 70 && avg <= 79) {
      console.log("C");
    } else if (avg >= 80 && avg <= 89) {
      console.log("B");
    } else if (avg >= 90 && avg <= 100) {
      console.log("A");
    }
  }

  console.log(calculateGrade(marks));

Following were the issues in your code

  1. You were not getting the parameters in function definition
  2. issues 2 ( <= should be < )

Upvotes: 1

loxxy
loxxy

Reputation: 13151

You just added an extra = in your for loop

i<=marks.length

instead of

i<marks.length

So while calculating the sum & average, a garbage value gets added up.

Upvotes: 1

Related Questions