我有這個函數,它接受'1'的字符串值併爲其添加'0'。如何循環遍歷一個函數一組次數?
問題是我希望它通過該功能設定的次數。 即我希望它去6次,所以數字結束爲'1''0''1''0''1''0',然後使用連接函數將它們作爲一個數組中的一個單一值
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.
}
'回報num.join();'了'for'循環外。現在它在循環的第一次迭代結束時返回 – tymeJV