2017-03-14 62 views
0

你好,我有一個「hello」數組和「」裏面的空字符串「」。問題是我希望能夠在數組中打印5次hello字符串,但是我正在運行一個無限循環。我設置了隨機函數和一個計數器,因爲我並不總是想知道我會得到什麼參數,但結果應該是相同的:打印5次「hello」。遞歸函數,直到計數器等於5無限循環

這是我的代碼:

var a = ["hello", ""]; 
 
var randomValue = a[Math.floor(a.length * Math.random())]; 
 

 
function toresult(param){ 
 
    let counter= 1; 
 

 
    if(param.length >=3 && counter <= 5){ 
 
     console.log(param) 
 
     counter +=1 
 

 
     //If I place the function here I would run into the infinite loop: toresult(randomValue) 
 

 
    } else{ 
 
     console.log("empty string PRINTED") 
 
    } 
 
} 
 

 
toresult(randomValue)

+2

是否有一個原因,你沒有使用實際的'for'循環? – larz

+2

發生無限循環是因爲您沒有提供退出子句。現在您每次調用函數時都會重新定義計數器變量。這是你需要解決的部分。如果它沒有傳遞給函數,你可以通過初始化來解決這個問題。 – scrappedcola

+0

@scrappedcola你應該只是回答,這是正確的答案,計數器需要在函數之外。 – Andrew

回答

2

無限循環,因爲你不提供一個退出條款。現在您每次調用函數時都會重新定義計數器變量。這是你需要解決的部分。如果它沒有傳遞給功能https://jsfiddle.net/rfbhk7de/,你可以通過初始化來解決這個問題。

var a = ["hello", ""]; 
var counter = 1; 
var randomValue = a[Math.floor(a.length * Math.random())]; 

function toresult(param){ 

    if(param.length >=3 && counter <= 5){ 

    console.log("Yes, inside!!") 
    counter +=1 

    toresult(randomValue) 

    }else{ 
    console.log("empty string PRINTED") 

    } 
} 

toresult(randomValue) 

另一種選擇是也通過計數器變量中,如果沒有在通過使用一個默認值計數器

var a = ["hello", ""]; 
var randomValue = a[Math.floor(a.length * Math.random())]; 

function toresult(param, counter){ 
    counter = typeof(counter) !== "undefined" ? counter : 1; //fancy way of doing an if loop. Basically says if counter is defined then use the passed in counter else set to default 1 
    if(param.length >=3 && counter <= 5){ 

    console.log("Yes, inside!!") 
    counter +=1 

    toresult(randomValue, counter) 

    }else{ 
    console.log("empty string PRINTED") 

    } 
} 

toresult(randomValue) 

第二實例的的jsfiddle:https://jsfiddle.net/yd1cxbvc/

+0

第一個不工作... – Defoe

+0

移除了評論之外的函數調用。現在應該是好的 – scrappedcola

-1

遞歸函數定義具有一個或多個基站的情況下,這意味着 輸入(S)的量,功能產生的結果(無 重複出現)以及一個或多個遞歸情況,這意味着輸入 其中pr ogram再次出現(調用本身)

你要的參數仔細傳遞到相同的函數調用TERMIT遞歸,如下圖所示:發生

var a = ["hello", ""]; 

function toresult(count){ 
    if(count < 1) return; 
    else { 
     a.push('hello'); 
     return toresult(count-1); 
    } 
} 
toresult(5); 


console.log(a); // ["hello", "", "hello","hello","hello","hello","hello"] 
+0

此答案是不完整的,因爲您要刪除隨機的空字符串,並且希望將其考慮在內,而不是將其打印出來,而是在發佈時考慮它。 – Defoe

+0

不幸的是,你的要求並不清楚你的代碼的整個目的。此外,此代碼的目的是爲您提供一個基礎,並不是一個完整的解決方案 –

0

我不認爲遞歸對這件事很好。至於我明白你想要做隨機的東西,但在fundomiser返回1的情況下,因此值得一試做而

var a = ["hello", ""]; 
var counter = 0; 
do{ 
    randomValue = a[Math.floor(a.length * Math.random())] 
    if(randomValue.length >=3){ 
     console.info(randomValue); 
     counter++; 
    }else{ 
     console.log("empty string PRINTED") 
    } 
}while(counter < 5) 

確定。儘管你已經有了答案。 這裏是基於遞歸的解決方案。

var a = ["hello", ""]; 

function doPrint(counter){ 
    randomValue = a[Math.floor(a.length * Math.random())] 
    if(!counter) return; 
    if(randomValue.length >=3 && counter--){ 
     console.info(randomValue); 

    }else{ 
     console.log("empty string PRINTED") 
    } 
    doPrint(counter); 
} 

doPrint(5); 
+0

你的代碼也在打印其他條件裏面...我不想那 – Defoe

+0

你能編輯與要求的問題,請。我的代碼是複製你的,但不使用遞歸。 – volkinc

+0

我需要使用遞歸,但在特定情況下可能不需要遞歸,我需要它用於異步目的 – Defoe