谁拥有最多的钱-Codewars挑战-JavaScript

时间:2019-11-15 21:42:09

标签: javascript arrays class instance

Link to challenge

  

您要与一些学生一起旅行,并且要由您来跟踪每个学生有多少钱。这样定义一个学生:

class Student {
  constructor(name, fives, tens, twenties) {
    this.name = name;
    this.fives = fives;
    this.tens = tens;
    this.twenties = twenties;
  }
}
  

正如您所知道的,每个学生都有五,五,二十岁。你的工作是返回钱最多的学生的名字。如果每个学生的金额都相同,则返回“全部”。

     

注意:

     

每个学生都有一个唯一的名字

     

总会有一个明确的赢家:一个人最多,或者每个人都有相同的金额

     

如果只有一个学生,则该学生的钱最多


我已经尝试过:

function mostMoney(students) {
  //get array of totals
  let array = [];
  students.forEach((value, index) => {
     let total = ((5 * value.fives) + (10 * value.tens) + (20 * value.twenties));
     array.push([total, value.name]);
  });
  //sort array of totals
  array = array.sort((a, b) => b[0] - a[0]);
  console.log('array****', array);
  //check if all totals are equal - if they are, return 'all'
  if (array.every((el, i, array) => (el)[0]) === array[0][0]) {
    return 'all'; 
  }
  else {
    return array[0][1];
  }
}

对我来说没有意义的是,当我在代码战中console.log('array****', array);时,它看起来像:

array**** [ [ 50, 'Eric' ],
  [ 40, 'Andy' ],
  [ 40, 'Stephen' ],
  [ 40, 'Phil' ],
  [ 30, 'David' ] ]
array**** [ [ 50, 'Eric' ],
  [ 40, 'Andy' ],
  [ 40, 'Stephen' ],
  [ 40, 'Phil' ],
  [ 30, 'Cameron' ],
  [ 30, 'Geoff' ],
  [ 30, 'David' ] ]
array**** [ [ 40, 'Andy' ] ]
array**** [ [ 40, 'Stephen' ] ]
array**** [ [ 30, 'Cameron' ], [ 30, 'Geoff' ] ]

为什么看起来像这样?我认为排序后,我的console.log('array***', array)应该像这样:

array**** [ [ 50, 'Eric' ],
  [ 40, 'Andy' ],
  [ 40, 'Stephen' ],
  [ 40, 'Phil' ],
  [ 30, 'Cameron' ],
  [ 30, 'Geoff' ],
  [ 30, 'David' ] ]

最初console.log(students)时,它看起来像一个数组:

[ Student { name: 'Andy', fives: 0, tens: 0, twenties: 2 },
  Student { name: 'Stephen', fives: 0, tens: 4, twenties: 0 },
  Student { name: 'Eric', fives: 8, tens: 1, twenties: 0 },
  Student { name: 'David', fives: 2, tens: 0, twenties: 1 },
  Student { name: 'Phil', fives: 0, tens: 2, twenties: 1 } ]

所以我试图用我的forEach循环来收集数组中的所有总数,然后在循环后对该数组进行排序-这种逻辑怎么了?

enter image description here

2 个答案:

答案 0 :(得分:1)

工作解决方案:)

function mostMoney(students) {
  let array = [];
  if (students.length === 1) {
     return students[0].name;
  }
  students.forEach((value, index) => {
     let total = ((5 * value.fives) + (10 * value.tens) + (20 * value.twenties));
     array.push([total, value.name]);
  });
  array = array.sort((a, b) => b[0] - a[0]);
  if (array.every((el, i, array) => el[0] === array[0][0])) {
    return 'all'; 
  }
  else {
    return array[0][1];
  }
}

实际上我的.every出了问题-我在做(el)[0])而不是el[0],然后我也没有正确检查只有一名学生通过时进入mostMoney

谢谢大家在console.log问题上的发言。 Codewars多次进行console.logging,因为,正如大家都提到的,它正在运行多个测试。

答案 1 :(得分:1)

我可以提出解决方案:

  function mostMoney(students) {
     //deep copy of argument
     let input = [...students];
     // sort students by total descending
     let sum = st => st.fives * 5 + st.tens * 10 + st.twenties * 20;
     let comparator = (st1,st2) => sum(st2) - sum(st1);
     input.sort(comparator);     
     // result
     //just compare the first two students
     if(input.length >=2 && sum(input[0]) == sum(input[1])){
      return 'all';
     }
     else{
      return input[0].name;
     }
  }