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

标签 javascript arrays class instance

Link to challenge

You're going on a trip with some students and it's up to you to keep track of how much money each Student has. A student is defined like this:

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

As you can tell, each Student has some fives, tens, and twenties. Your job is to return the name of the student with the most money. If every student has the same amount, then return "all".

Notes:

Each student will have a unique name

There will always be a clear winner: either one person has the most, or everyone has the same amount

If there is only one student, then that student has the most money



我试过这个:

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

最佳答案

工作解决方案:)

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 多次,因为正如你们所提到的,它正在运行多个测试。

关于javascript - 谁拥有最多的钱 - Codewars 挑战 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58885076/

相关文章:

arrays - 在单独的 ViewController 中编辑 tableView 条目,并将编辑后的数据传回 tableView

c++ - 在 ')' token 之前应为 '*'

javascript - IE8 中的 targetElem.innerHTML 未知运行时错误

javascript - jquery将dblclick事件绑定(bind)到多个元素

javascript - 尝试从 JavaScript 向 CSS 传递参数

javascript - 在 Angular 2 上使用 *ngFor 在多行中显示来自 @ContentChildren 的组件内容

c - 有没有一种简洁的方法(没有循环)在 C 中创建线性间隔的数组?

java - 在Java中,我如何在函数 "1"(不使用参数)中使用在函数 "A"内部生成的数组 "B"?

C#:嵌套类的构造函数使 "inaccessible due to protection level"

Objective-c 枚举类