fapchat
@fapchat

Где ошибка в коде?

var products = [ { name: "Grapefruit", calories: 170, color: "red", sold: 8200 },
 { name: "Orange", calories: 160, color: "orange", sold: 12101 },
 { name: "Cola", calories: 210, color: "caramel", sold: 25412 },
 { name: "Diet Cola", calories: 0, color: "caramel", sold: 43922 },
 { name: "Lemon", calories: 200, color: "clear", sold: 14983 },
 { name: "Raspberry", calories: 180, color: "pink", sold: 9427 },
 { name: "Root Beer", calories: 200, color: "caramel", sold: 9909 },
 { name: "Water", calories: 0, color: "clear", sold: 62123 }
 ];
 
function compareName(colaA, colaB) {
  if (colaA.name > colaB.name) {
  return 1;
  } else if (colaA.name === colaB.name) {
  return 0;
  } else {
  return -1;
  }
}
function compareCalories(colaA, colaB) {
  if (colaA.name > colaB.name) {
  return 1;
  } else if (colaA.name === colaB.name) {
  return 0;
  } else {
  return -1;
  }
}
function compareColor(colaA, colaB) {
  if (colaA.color > colaB.color) {
  return 1;
  } else if (colaA.color === colaB.color) {
  return 0;
  } else {
  return -1;
  }
}


function printProducts(products) {
 for (var i = 0; i < products.length; i++) {
 console.log("Name: " + products[i].name +
 ", Calories: " + products[i].calories +
 ", Color: " + products[i].color +
 ", Sold: " + products[i].sold);
 }
}

products.sort(compareName);
console.log("Products sorted by name:");
printProducts(products);

 products.sort(compareCalories);
console.log("Products sorted by calories:");
printProducts(products);

  products.sort(compareColor);
console.log("Products sorted by color:");
printProducts(products);


Вот что в консоли
Products sorted by name:
compareSold2.html:48 Name: Cola, Calories: 210, Color: caramel, Sold: 25412
compareSold2.html:48 Name: Diet Cola, Calories: 0, Color: caramel, Sold: 43922
compareSold2.html:48 Name: Grapefruit, Calories: 170, Color: red, Sold: 8200
compareSold2.html:48 Name: Lemon, Calories: 200, Color: clear, Sold: 14983
compareSold2.html:48 Name: Orange, Calories: 160, Color: orange, Sold: 12101
compareSold2.html:48 Name: Raspberry, Calories: 180, Color: pink, Sold: 9427
compareSold2.html:48 Name: Root Beer, Calories: 200, Color: caramel, Sold: 9909
compareSold2.html:48 Name: Water, Calories: 0, Color: clear, Sold: 62123
compareSold2.html:60 Products sorted by calories:
compareSold2.html:48 Name: Cola, Calories: 210, Color: caramel, Sold: 25412
compareSold2.html:48 Name: Diet Cola, Calories: 0, Color: caramel, Sold: 43922
compareSold2.html:48 Name: Grapefruit, Calories: 170, Color: red, Sold: 8200
compareSold2.html:48 Name: Lemon, Calories: 200, Color: clear, Sold: 14983
compareSold2.html:48 Name: Orange, Calories: 160, Color: orange, Sold: 12101
compareSold2.html:48 Name: Raspberry, Calories: 180, Color: pink, Sold: 9427
compareSold2.html:48 Name: Root Beer, Calories: 200, Color: caramel, Sold: 9909
compareSold2.html:48 Name: Water, Calories: 0, Color: clear, Sold: 62123
compareSold2.html:64 Products sorted by color:
compareSold2.html:48 Name: Cola, Calories: 210, Color: caramel, Sold: 25412
compareSold2.html:48 Name: Diet Cola, Calories: 0, Color: caramel, Sold: 43922
compareSold2.html:48 Name: Root Beer, Calories: 200, Color: caramel, Sold: 9909
compareSold2.html:48 Name: Lemon, Calories: 200, Color: clear, Sold: 14983
compareSold2.html:48 Name: Water, Calories: 0, Color: clear, Sold: 62123
compareSold2.html:48 Name: Orange, Calories: 160, Color: orange, Sold: 12101
compareSold2.html:48 Name: Raspberry, Calories: 180, Color: pink, Sold: 9427
compareSold2.html:48 Name: Grapefruit, Calories: 170, Color: red, Sold: 8200
  • Вопрос задан
  • 102 просмотра
Решения вопроса 1
sfi0zy
@sfi0zy Куратор тега JavaScript
Creative frontend developer
function compareCalories(colaA, colaB) {
    if (colaA.name > colaB.name) {
        return 1;
    } else if (colaA.name === colaB.name) {
        // . . .

Может тут нужно сравнивать calories, а не name у продуктов?
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
21 нояб. 2024, в 23:30
300000 руб./за проект
21 нояб. 2024, в 22:21
3000 руб./в час
21 нояб. 2024, в 21:42
100000 руб./за проект