简体   繁体   English

谁的钱最多 - Codewars 挑战 - JavaScript

[英]Who Has the Most Money - Codewars Challenge - JavaScript

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.如您所知,每个学生都有一些 5、10 和 20。 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如果只有一个学生,那么那个学生的钱最多


I've tried this:我试过这个:

 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]; } }

What doesn't make sense to me is that when I console.log('array****', array);对我来说没有意义的是,当我console.log('array****', array); in codewars it looks like:在代码战中它看起来像:

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' ] ]

Why does it look like that?为什么会这样? I would think that after sorting, my console.log('array***', array) should just look like:我认为排序后,我的console.log('array***', array)应该看起来像:

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

When I initially console.log(students) , it looks like an array:当我最初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 } ]

So I'm trying to collect all of the totals in an array with my forEach loop, and then sorting that array after looping - what's wrong with that logic?所以我试图用我的forEach循环收集一个数组中的所有总数,然后在循环后对该数组进行排序——这个逻辑有什么问题?

在此处输入图像描述

Working solution: )工作解决方案:)

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];
  }
}

There was in fact a problem with my .every - I was doing (el)[0]) instead of el[0] , and then I also wasn't properly checking for when there was only one student passed into mostMoney .实际上,我的.every存在问题 - 我正在做(el)[0])而不是el[0] ,然后我也没有正确检查何时只有一个学生传递给mostMoney

Thanks all for shedding light on the console.log issue.感谢大家阐明console.log 问题。 Codewars was console.logging multiple times because, as you all mentioned, it's running multiple tests. Codewars 是 console.logging 多次,因为正如你们所提到的,它正在运行多个测试。

I can suggest as solution:我可以建议作为解决方案:

  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;
     }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM