简体   繁体   English

为什么我的带有数组的JavaScript代码不起作用?

[英]Why is my JavaScript code with arrays not working?

I am trying to solve the CodeWars Kata: Zeros and Ones. 我正在尝试解决CodeWars Kata:零和一。 My code solves over 1800 of the Attempt tests and fails just over 100. My problem is that I cannot see the test data because it is so large. 我的代码解决了1800多个Attempt测试,失败了100多个。我的问题是我看不到测试数据,因为它太大了。 To see the test data in other katas I can use a console.log statement. 要查看其他选项卡中的测试数据,我可以使用console.log语句。 But, the test data in this challenge is too large. 但是,挑战中的测试数据太大。 I get a list of the first part of an input array followed by "and 400 more items." 我得到一个输入数组的第一部分的列表,后跟“和另外400个项目”。

Below is a description of the kata and my code along with some test cases. 下面是对kata和我的代码以及一些测试用例的描述。 I have tried to create test cases that fail when my code runs them; 我试图创建在我的代码运行它们时失败的测试用例。 but, I am unable create any that fail. 但是,我无法创建任何失败的对象。

  • Is my logic sound? 我的逻辑健全吗? Am I missing anything in the logic? 我在逻辑上缺少什么吗?
  • Can you supply a test case that would fail? 您能否提供一个失败的测试用例? (That would be better to help me learn as I would have to analyze my code to fix it.) (这将更好地帮助我学习,因为我必须分析我的代码以对其进行修复。)

Given an array containing only zeros and ones, find the index of the zero that, if converted to one, will make the longest sequence of ones. 给定一个仅包含零和一的数组,请找到零的索引,如果将其转换为一,将形成最长的一序列。 For instance, given the array... [1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1], ...replacing the zero at index 10 (counting from 0) forms a sequence of 9 ones. 例如,给定数组... [1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1],。 ..在索引10处替换零(从0开始计数)形成9的序列。 Your task is to write the function replaceZero() that determines where to replace a zero with a one to make the maximum length sub sequence. 您的任务是编写函数replaceZero(),该函数确定将零替换为1的位置,以形成最大长度的子序列。 Note: If the are multiple results, return the last [1,1,0,1,1,0,1,1] //=> 5 The array will always contain zeros and ones. 注意:如果有多个结果,则返回最后一个[1,1,0,1,1,0,1,1] // => 5数组将始终包含零和一。

function replaceZero(arr){

var highestCt = 0;
var returnIdx = 0;

var onesCt = 0;
var currentHighOnesCt = 0;

function countOnes(currentIZ){ // The input should be the current index of the ZERO.
  for(var j = currentIZ - 1; j >= 0; j--){ // Go backwards until a zero is found, or to the beginning of the array.
    if(arr[j] === 1){
      onesCt = onesCt + 1;
    }
    else { // if arr[j] === 0
      break;
    }
  }
  for(var k = currentIZ + 1; k < arr.length; k++){ // Go forwards to a zero or the end of the array.
    if(arr[k] === 1){
      onesCt = onesCt + 1;
    }
    else { // if arr[k] === 0
      break;
    }
  }
  if(onesCt >= currentHighOnesCt){
    currentHighOnesCt = onesCt;
    returnIdx = currentIZ;
  }
  onesCt = 0;
}

for(var i = 0; i < arr.length - 2; i++)
  if(arr[i] === 1 && arr[i + 1] === 0 && arr[i + 2] === 1){
    countOnes(i + 1); // Send the index of the ZERO.
  }

// console.log("returnIdx: " + returnIdx);

return returnIdx;
}

/*
replaceZero([0,0,1,1,1,0,1,1,0,0,1,0]); // 5
replaceZero([0,0,0,1,1,1,0,1,1,0,0,0]); // 6
replaceZero([1,1,1,1,0,1]); // 4
*/
replaceZero([1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1]); // 9
replaceZero([1,0,0,0,1,0,1,1]); // 5

/*
replaceZero([0,0,1,1,1,1,0,1,1,1,0,1,1,0,0,1,1,0,1,1,0,1,1]); // 6
replaceZero([0,0,1,0,0,1,1,1,0,1,1,0,0,1,1,0,1,1,0]); // 8
replaceZero([0,0,1,0,0,1,1,0,1,1,0,0,1,1,1,0,1,1,0]); // 15
replaceZero([1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1]); // 10;
replaceZero([1,1,0,1,1,0,1,1]); // 5;
replaceZero([1,1,1,0,1,1,0,1,1,1]); // 6);
*/

Alright so, i'm not actually going to refactor your code, but this might help you: 好的,我实际上不会重构您的代码,但这可能对您有所帮助:

  • Why do you assume the Zero you have to change is in between two Ones ? 为什么你假设Zero ,你必须二者之间的变化是Ones your if statement is flawed in that matter. 您的if陈述在这件事上有缺陷。

your code: 您的代码:

if(arr[i] === 1 && arr[i + 1] === 0 && arr[i + 2] === 1){ //WHY IN BETWEEN??
  countOnes(i + 1); // Send the index of the ZERO.
}
  • Having the above in mind, the following scenarios will fail: 考虑到上述情况,以下情况将失败:

Code: 码:

replaceZero([0,0,0,0,0,0,1]); // 0
replaceZero([0,0,0,0,0,1,1]); // 0
replaceZero([0,0,0,0,1,1,1]); // 0
//.. and so on
//Also
replaceZero([1,1,1,1,1,0,0]); // 0
replaceZero([1,1,1,1,0,0,0]); // 0

and don't be surprised if there is a test for [0,0,0,0,0,0,0] in this case the last index in the array is the expected answer. 如果对[0,0,0,0,0,0,0]进行测试,也不要感到惊讶,在这种情况下,数组中的最后一个索引就是预期的答案。

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

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