简体   繁体   English

观鸟者练习 Javascript 解决方案 - 计数器不计数

[英]Bird Watcher Exercism Javascript Solution - counter not counting

I'm trying to figure out an exercise from Exercism's Javascript track.我正在尝试从 Exercism 的 Javascript 曲目中找出一个练习。 It is an exercise using for loops called Bird Watcher.这是一个使用称为 Bird Watcher 的 for 循环的练习。

The instructions say to initialize a function called birdsInWeek that takes two arguments.指令说要初始化一个名为birdInWeek的function,它需要两个arguments。 First is an array 'birdsPerDay', and the second is 'week'.第一个是数组'birdsPerDay',第二个是'week'。 Given the birdsPerDay array, the function is supposed to count the total number of birds seen in the specified week.给定birdsPerDay 数组,function 应该计算指定周内看到的鸟类总数。 So, if 'week' = 1, for example, the corresponding indexes of 'birdsPerDay' to add together would be 0 - 6, and so on and so forth.因此,如果'week' = 1,例如,'birdsPerDay' 的相应索引加在一起将是 0 - 6,依此类推。

I have tested my code using the provided test cases and in the Chrome console.我已经使用提供的测试用例和 Chrome 控制台测试了我的代码。 I tried logging some values to understand where the bug is, but I can't figure out why my counter (named 'totalBirdsInSpecifiedWeek'), which is initialized to '0' is staying at '0' instead of adding the corresponding indexes of 'week' together and return the correct 'totalBirdsInSpecifiedWeek'.我尝试记录一些值以了解错误在哪里,但我无法弄清楚为什么初始化为“0”的计数器(名为“totalBirdsInSpecifiedWeek”)停留在“0”而不是添加相应的索引“ week' 一起返回正确的 'totalBirdsInSpecifiedWeek'。 I have tried changing the placement of the return statement as well, but that didn't result in any change.我也尝试过更改 return 语句的位置,但这并没有导致任何更改。

Below is the code I have written:下面是我写的代码:

export function birdsInWeek(birdsPerDay, week) {
  let totalBirdsInSpecifiedWeek = 0
  if (week === 1) {
    for (let i = 0; i < birdsPerDay[7]; i++) {
      totalBirdsInSpecifiedWeek += birdsPerDay[i];
    } 
    return totalBirdsInSpecifiedWeek;
  } else {
  for (let i = week * 7 - 7; i < birdsPerDay[week * 7]; i++) {
    totalBirdsInSpecifiedWeek += birdsPerDay[i];
  }
  return totalBirdsInSpecifiedWeek;
};
}

Where did I go wrong?我在哪里 go 错了?

birdsPerDay[7] this means values of birdsPerDay array at index 7 of the array so the condition would loop checking if i is lesser than that value sometimes it would even hit the NaN so the idea is to just check on either length or index of the array to get an accurate response.. BirdPerDay[7] 这意味着数组索引 7 处的birdPerDay 数组的值,因此条件会循环检查 i 是否小于该值,有时它甚至会达到 NaN,所以我们的想法是只检查长度或索引阵列以获得准确的响应..

Per the commenter @gog, I changed the 4th and 9th lines of code to correct the stopping condition in each of the for loops so they are written as indexes of the 'birdsPerDay' array and instead just numerical values.根据评论者@gog,我更改了第 4 行和第 9 行代码以更正每个 for 循环中的停止条件,因此它们被写为“birdsPerDay”数组的索引,而不是数值。

export function birdsInWeek(birdsPerDay, week) {
  let totalBirdsInSpecifiedWeek = 0
  if (week === 1) {
    for (let i = 0; i < 7; i++) {
      totalBirdsInSpecifiedWeek += birdsPerDay[i];
    } 
    return totalBirdsInSpecifiedWeek;
  } else {
  for (let i = week * 7 - 7; i < week * 7; i++) {
    totalBirdsInSpecifiedWeek += birdsPerDay[i];
  }
  return totalBirdsInSpecifiedWeek;
}; return totalBirdsInSpecifiedWeek;
}

I found the exercise, according to the problem posed, I think this is the solution.我找到了练习,根据提出的问题,我认为这是解决方案。 bird-watcher - exercism 观鸟者 - 运动

Explanation, the array is divided by the days of the week, then the array is divided by that amount from 0 to 6 (Monday to Sunday), then I used a nested "for" to iterate over the corresponding week, I used another "for" to do the addition, return the value from the function and return the result.解释,数组除以星期几,然后数组除以从 0 到 6(周一到周日)的数量,然后我使用嵌套的“for”来迭代相应的周,我使用了另一个“ for" 进行加法,从 function 返回值并返回结果。

let countsbirdsPerDay = [2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1];
let userWeek = 2;

const birdsInWeek = (birdsPerDay, week) => {
    let segmentWeek = Math.round(birdsPerDay.length / week);
    let total = 0;
    for (let i = 0; i < week; i++) {
        views = birdsPerDay.slice(i * segmentWeek, segmentWeek * (i + 1));
        if ((i + 1) == week) {
            for (let x = 0; x < views.length; x++) {
                total = total + views[x];
            }       
        }
    }
    return total;
}

console.log(birdsInWeek(countsbirdsPerDay, userWeek));

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

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