0

I'm a beginner in JS and trying to sort some cars by their model. The models are sorted by ranking in this order (Mercedes, BMW, Jeep, Nissan). I would like it to be case-insensitive. I went about it by creating a variable for creating the desired rankings.

var modelRanking = function(car) {
  if (car.model.toLowerCase() === 'mercedes') {
      return 1;
  } else if (car.model.toLowerCase() === 'bmw') {
      return 2;
  } else if (car.model.toLowerCase() === 'jeep') {
     return 3;
  } else if (car.model.toLowerCase() === 'nissan') {
     return 4;
  } else {
     return 5;
  }
}

function modelComparator(car1, car2) {
  if (car1.modelRanking < car2.modelRanking) {
     return true;
  } else if (car1.modelRanking > car2.modelRanking) {
     return false;
  } else if (car1.modelRanking == car2.modelRanking) {
     return yearComparator(car1, car2);
  }
}

However the modelRanking is always returning 5.

2
  • 1
    Where are you using the modelRanking function. Does that property exist? Commented Jul 14, 2020 at 2:40
  • you should pass your car as argumment: modelRanking(car1) modelRanking(car2) Commented Jul 14, 2020 at 2:43

1 Answer 1

1

Instead of car1.modelRanking, use modelRanking(car1) because modelRanking is a function in global scope, not a property of car1.

function modelComparator(car1, car2) {
  if (modelRanking(car1) < modelRanking(car2)) {
     return true;
  } else if (modelRanking(car1) > modelRanking(car2)) {
     return false;
  } else if (modelRanking(car1) == modelRanking(car2)) {
     return yearComparator(car1, car2);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow can't believe I did that. Seems like my mind is still stuck in Python. Thanks a bunch!

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.