Getting an array list of scores

I am trying to create a list of numbers so that I can pass it through the assignGrade function. I want to put them in an array and then run them through to get a result of A,B,C,D,F. I think that I am close but don’t know what else to try.

var assignGrade = function assignGrade(score) {
    if (score > 90) {
        return 'A';
    } else if (score > 80) {
        return 'B';
    } else if (score > 70) {
        return 'C';
    } else if (score > 65) {
        return 'D';
    } else {
        return 'F';
    }
}

function getScore(result) {
    result = [];
    for (var i = 0; i < result.length; i++) {
    assignGrade;
    }
    return result;
}

console.log(getScore(55,77,88));

Answer

You were pretty close, you just need to change couple of things

var assignGrade = function assignGrade(score) {
    if (score > 90) {
        return 'A';
    } else if (score > 80) {
        return 'B';
    } else if (score > 70) {
        return 'C';
    } else if (score > 65) {
        return 'D';
    } else {
        return 'F';
    }
}

function getScore(result) {
    //result = []; <- no to format array
    for (var i = 0; i < result.length; i++) {
         //https://jsfiddle.net/0m8e670q/1/ you have just called the function with no value and didnt store the returned value
         result[i] = assignGrade(result[i]);//you need to send the function a value and then store it
    }
    return result;
}

console.log(getScore([55,77,88]));//send array

fiddle – https://jsfiddle.net/0m8e670q/1/

Leave a Reply

Your email address will not be published. Required fields are marked *