简体   繁体   中英

Why I am not getting any output?

function printManyTimes(str) {
       var sentence = str + "is cool"
           for (var i = 0; i < str.length; i += 2) {
               console.log(sentence);
}

printManyTimes("Satyam")
}

I am not getting any output. Result is blank, what am I doing wrong?

If you indent your code correctly, you see, that you call the function inside itself, not after it:

function printManyTimes(str) {
    var sentence = str + "is cool"
    for (var i = 0; i < str.length; i += 2) {
        console.log(sentence);
    }
    printManyTimes("Satyam")
}

So, it won't run at all - and if you would try it, you would get a lot of logs printed in the console, and a stack overflow (not the site) error.

It should be like that:

function printManyTimes(str) {
    var sentence = str + "is cool"
    for (var i = 0; i < str.length; i += 2) {
        console.log(sentence);
    }
}

printManyTimes("Satyam")

the second closing bracket is after the function call - technically you defined a recursive function.

You forgot to close bracket of for cycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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