[英]How to loop through a function a set number of times?
I have this function that takes the string values of '1' and adds a '0' to it. 我有这个函数,它接受字符串值“ 1”并为其添加“ 0”。
The problem is that I want it to go through the function a set number of times. 问题是我希望它通过该函数一定次数。 ie I want it to go 6 times so the number ends up being '1' '0' '1' '0' '1' '0' then using the join function to have them as one single value in an array
即我希望它去6次,所以数字最终为'1''0''1''0''1''0',然后使用join函数将它们作为数组中的单个值
alert(stringy()); //write a function that names stringy function stringy(size) { //create and empty array to hold new value var num = [] //push a the string value of 1 into array num.push('1') //loop through the array and if the variable equals 1 then push a 0 for (i = 0; i < num.length; i++) { if (num[i] == '1') { num.push('0') //if num has 0 in in it push 1 } else if (num[i] == '0') { num.push('1') } //numbers return two values in array but does not specify how many goes in. return num.join(); } //incomplete: only shows '1,0' in output one time. }
You need a condition before returning, checking that the array size is equal to what is asked for: 返回之前,您需要一个条件,检查数组大小是否等于要求的大小:
if (num.length >= size) {
return num.join();
}
The >
is a safety to avoid infinite looping when size
is less than 2. Don't forget to pass a size
when you call the function. 当
size
小于2时, >
可以避免无限循环。不要忘记在调用函数时传递size
。
Note that it is bad practice to add to an array while iterating over it: this can be confusing and the cause of undetected bugs. 请注意,在数组上进行迭代时添加数组是不好的做法:这可能会造成混淆,并导致无法检测到错误。 I seem to understand that you got this as a template in a course or something.
我似乎了解您在课程或某些课程中将其作为模板。 If so, forget this way of working.
如果是这样,请忘记这种工作方式。
A better design for such a function is: 此类功能的更好设计是:
function stringy(size){ return Array.from(Array(size), (_, i) => 1-(i%2)).join(''); } console.log(stringy(5)); console.log(stringy(6));
And, there is even a String#repeat
function: 而且,甚至还有一个
String#repeat
函数:
function stringy(size){ return '10'.repeat(size/2+1).substr(0,size); } console.log(stringy(5)); console.log(stringy(6));
A couple problems: 几个问题:
You're not using the passed size
in your loop. 您没有在循环中使用传递的
size
。 And you're returning inside the for, which returns after just one iteration. 您将在for内部返回,仅在一次迭代后返回。
I think this is closer to what you want; 我认为这更接近您想要的;
function stringy(size){
var num=[]
num.push('1')
for(i=0; i < size; i++){
if(num[i]=='1'){
num.push('0')
} else if(num[i]=='0'){
num.push('1') }
}
return num.join();
}
You can also simplify things a bit which helps the readability: 您还可以简化一些事情,以提高可读性:
function stringy(size){
var num=[1]
for(i=0; i < size; i++){
num.push( num[i]==1 ? 0 : 1)
}
return num.join();
}
console.log(stringy(6))
function something(size) {
var iterations = Math.floor(size / 2);
var result = '';
for (var index = 0; index < iterations; index++) {
result += '10';
}
return result;
}
/**
* Use modulo operator:
* The modulo operator (%) returns the division remainder.
* So:
* 0/2 = 0
* 1/2 = 1
* 2/2 = 0
* 3/2 = 1
* 4/2 = 0
* @param size int Array size.
* @return string Joined array: "1,0,1,0,1,0,..."
*/
function stringy(size) {
var num = [];
for (var i = 0; i < size; i++) {
num.push((i % 2 === 0) ? 1 : 0);
}
return num.join();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.