How can I return the correct output from a function?

Viewed 36

I'm new to coding (2nd day!) and following on from a guided task on a testing website I've got the following code that isn't returning properly and for the life of me I can't figure out. I'm really bad with maths so that doesn't help but please bare with me.

Why is the following code not returning the correct information when passed an array of names.

function gatherFeedback (feedbackArray) {
    let positive = 0;
    let negative = 0;
    let neutral = 0;
    
    for (let i = 0; i < feedbackArray.length; i++) {
        if (feedbackArray[i][1] <= 10 && feedbackArray[i][1] > 6) {
            positive++;
        } else if (feedbackArray[i][1] <= 6 && feedbackArray[i][1] > 4) {
            neutral++;
        } else {
            negative++;
        }
    }
      
    let test = {'positive': positive, 'negative': negative, 'neutral': neutral}
      
    return test;
}

I pass the first two of three tests but the final one I'm stuck on.

gatherFeedback's output:

{ "positive": 2, "negative": 2, "neutral": 1 }

Output should be

{ "positive": 2, "negative": 1, "neutral": 2 }

It should return an object with three properties: a key of positive with a value of the number of positive reviews; a key of negative with a value of the number of negative reviews; and, a key of neutral with a value of the number of neutral reviews. Positive (7-10), negative (0-3) or neutral (4-6) The function gatherFeedback takes an array of arrays, each of these arrays contains both a string of the name of the attendee and a number showing how they rated the party out of 10

Example:

gatherFeedback([['maddie', 10], ['jatinder', 10], ['rose', 1]]);
// returns {positive: 2, negative: 1, neutral:0}
1 Answers
function gatherFeedback (feedbackArray){
let positive = 0;
let neutral = 0;
let negative = 0;
for (let i = 0; i<feedbackArray.length; i++)
if(feedbackArray[i][1] <= 3){ negative++; 
}else if(feedbackArray[i][1] <=6){neutral++;
} else {positive++;
}

return { 'positive': positive, "negative" : negative, "neutral" : neutral }; }

Related