简体   繁体   English

为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用?

[英]why my code doesn't work when I am trying to concatenate a function's return value with a string?

So, in this code I have a string of 0's and 1's and the length of the string is 32, which will be split in 6 equal parts but the last part will have the length of 2 so I will add (4) 0's after that which will make its length 6. So I wrote a function that will add the remaining 0's which is padding(num).因此,在这段代码中,我有一个由 0 和 1 组成的字符串,字符串的长度为 32,它将被分成 6 个相等的部分,但最后一部分的长度为 2,所以我将在之后添加 (4) 个 0这将使其长度为6。所以我写了一个function,它将添加剩余的0,即填充(num)。 And that function will be invoked in side the slicing(str) function.并且 function 将在切片(str)function 中调用。 But the code breaks when I try to do execute.但是当我尝试执行时代码会中断。 Any help?有什么帮助吗? Thanks.谢谢。

// This code works.


function padding0s(num) {
  let s = "";
  for (i = 0; i < 6 - num; i++) {
    s += "0";
  }
  return s;
}

function slicing(str) {
  let k = 6;
  let res = [];
  let temp1 = 0;
  let f = padding0s(2);
  for (i = 0; i < str.length; ) {
    res.push(str.slice(i, k));
    i += 6;
    k += 6;
    if (res[temp1].length !== 6) {
      res[temp1] += f;
    }
    temp1++;
  }
  console.log(res);
}

slicing("01000011010011110100010001000101");
    
// But this does not..


function padding0s(num) {
  let s = "";
  for (i = 0; i < 6 - num; i++) {
    s += "0";
  }
  return s;
}

function slicing(str) {
  let k = 6;
  let res = [];
  let temp1 = 0;
  
  for (i = 0; i < str.length; ) {
    res.push(str.slice(i, k));
    i += 6;
    k += 6;
    if (res[temp1].length !== 6) {
     let f = padding0s(res[temp1].length);
      res[temp1] += f;
    }
    temp1++;
  }
  console.log(res);
}

slicing("01000011010011110100010001000101");

Always define variables before using them始终在使用变量之前定义变量

Not doing so can result in undefined behaviour, which is exactly what is happening in your second case.不这样做会导致未定义的行为,这正是您的第二种情况。 Here is how:方法如下:

for (i = 0; i < str.length; ) {...}
  // ^ Assignment to undefined variable i

In the above for-loop, by using i before you define it, you are declaring it as a global variable.在上面的 for 循环中,通过在定义之前使用i ,您将其声明为全局变量。 But so far, so good, as it doesn't matter, if not for this second problem.但到目前为止,一切都很好,如果不是第二个问题,那也没关系。 The real problem is the call to padding0s() in your loop.真正的问题是循环中对padding0s()的调用。 Let's look at padding0s :让我们看一下padding0s

function padding0s(num) {
  ...
  for (i = 0; i < 6 - num; i++) {
    s += "0";
  }
}

This is another loop using i without defining it.这是另一个使用i而不定义它的循环。 But since i was already defined as a global variable in the parent loop, this loop will be setting its value.但是由于i已经在父循环中定义为全局变量,因此该循环将设置其值。 So in short, the value of i is always equal to 6 - num in the parent loop.所以简而言之, i的值在父循环中总是等于6 - num Since your exit condition is i < str.length , with a string of length 32 the loop will run forever.由于您的退出条件是i < str.length ,因此使用长度为 32 的字符串,循环将永远运行。

You can get around this in many ways, one of which you've already posted.您可以通过多种方式解决此问题,其中一种您已经发布过。 The other way would be to use let i or var i instead of i in the parent loop.另一种方法是在父循环中使用let ivar i而不是i Even better is to write something like this (but beware that padEnd may not work on old browsers ):更好的是写这样的东西(但要注意padEnd可能不适用于旧浏览器):

 function slicing(str) { return str.match(/.{1,6}/g).map((item) => { return item.padEnd(6, "0"); }); } console.log(slicing("01000011010011110100010001000101"));

暂无
暂无

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

相关问题 我正在尝试优化第一块代码。 为什么我的方法不起作用? 缩短代码的最佳方法是什么? - I am trying to optimize the first chunk of code. Why doesn't my approach work? What's the best approach to shorten the code? 为什么这个 JS 和 PHP 代码不起作用? (我正在尝试将值从 JS 发送到 PHP) - Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP) 我为函数的返回值得到一个空字符串 - I am getting a empty string for my function's return value 我正在编写一个Javascript函数来为表中的每一行着色不同的颜色。 为什么我的代码不起作用? - I am writing a Javascript function to to color every other row in a table a different color. Why doesn't my code work? 为什么当我调用具有返回值的函数时,我的onclick事件不起作用? - Why when i call a function with returning value, my onclick event doesn't work? 我尝试在共享点上突出显示我的表行,如果它与数组中的任何内容匹配,为什么不起作用? - I am trying to highlight my table row on sharepoint if it matches anything in an array, why doesn't it work? 尝试从函数返回字符串值并将其保存到 mongoDB 不起作用 - Trying to return a string value from a function and save it to mongoDB doesn't work 为什么在尝试运行我的代码时会收到 TypeError? - Why am I getting a TypeError when trying to run my code? 我试图让我的代码适用于单一输入,例如:当我输入 1 时,它应该返回 0 小时 1 分钟而不是 0 小时 1 分钟 - I am trying to make my code work for singular inputs eg: when i input 1 it should return 0 hours , 1 minute instead of 0 hours, 1 minutes 为什么我的reduce函数中的累加器不采用回调的返回值? - Why doesn't the accumulator in my reduce function take on the callback's return value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM