0

Self teaching myself development/ES6 and having some trouble with this question I’ve been trying to work through for a little while now.

I have grades of students, and the grade threshold. Trying to return an array that contains the student’s name, place in the array, and their score converted into a letter grade.

I have the switch statement, I’m just struggling with how to use it to return an object with student name, index in the array, and letter grade, as well as how to do this with the const and fat arrow syntax.

Any help/guidance would be appreciated.

Thanks

const scoreGrades = {
  A: 100,
  B: 90,
  C: 80,
  D: 70,
  F: 60,
}

const gradeReport = [{
  name: ‘Abby’,
  mark: 20,
}, {
  name: ‘Brian’,
  mark: 100,
}, {
  name: ‘Chella’,
  mark: 60
}];



const studentResults = (gradeReport, scoreGrades) => {

function getstudentMarks (result) {

  var mark = "";

  switch(true) {
    case mark < 60: 
      grade = "F";
      break;
    case mark < 70: 
      grade = "D";
      break;
    case mark < 80: 
      grade = "C";
      break;
    case mark < 90: 
      grade = "B";
      break;
    case mark <= 100:
      grade = "A";
      break;
  }


  return mark;
}


var i = 1;
while(i > 0 && <= 100){
  console.log(i, 'should be', getstudentMarks(i));
  i += 5;
}





}
3
  • 1
    "return an array that contains the student’s name, place in the array, and their score converted into a letter grade." This sounds like it would be more properly returned as an object, not an array. Commented Aug 11, 2018 at 16:56
  • @Code-Apprentice yes, you're right, that's what i meant (as indicated in the title), i've edited to show this, thanks Commented Aug 11, 2018 at 16:58
  • 1
    So what is the problem? Commented Aug 11, 2018 at 16:59

2 Answers 2

1

I suggest you use map method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map.

Also, switch doesn't work like that. In a simplest implementation you could just use a series of if/else ifs.

Here's a working example:

function getGrade(mark) {
  if (mark < 60) {
    return "E";
  } else if (mark < 70) {
    return "D";
  } else if (mark < 80) {
    return "C";
  } else if (mark < 90) {
    return "B";
  } else {
    return "A";
  }
}

function getResults(scores) {
  return scores.map((score, index) => ({
    index: index,
    name: score.name,
    grade: getGrade(score.mark)
  }));
}

const studentScores = [{
  name: 'Abby',
  mark: 20,
}, {
  name: 'Brian',
  mark: 100,
}, {
  name: 'Chella',
  mark: 60
}];

console.log(getResults(studentScores));

The result is:

[
    {"index":0,"name":"Abby","grade":"E"}, 
    {"index":1,"name":"Brian","grade":"A"}, 
    {"index":2,"name":"Chella","grade":"D"}
]
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this, very helpful! reading over the article makes it a lot more clear why the map method is a good choice.
0
const scoreGrades = {
A: 100,
B: 90,
C: 80,
D: 70,
F: 60,
}

const gradeReport = [{
  name: 'Abby',
  mark: 20,
}, {
  name: 'Brian',
  mark: 100,
}, {
  name: 'Chella',
  mark: 60
}];



const getStudentResults = (gradeReport, scoreGrades) => {
  let studentResults = []
  gradeReport.forEach(student=>{
      let grade;

      if (student.mark >= 90) {
        grade = 'A';
      } else if (student.mark >= 80) {
        grade = 'B';
      } else if (student.mark >= 70) {
        grade = 'C';
      } else if (student.mark >= 60) {
        grade = 'D';
      } else {
          grade = 'F'
      } 

      studentResults.push({name:student.name, grade:grade});
  })

  return studentResults;
}

const studentResults = getStudentResults(gradeReport, scoreGrades);

console.log(studentResults);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.