JustAGuy
JustAGuy

Reputation: 25

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?

Upvotes: 0

Views: 36

Answers (2)

FZs
FZs

Reputation: 18619

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")

Upvotes: 1

Ondřej Navr&#225;til
Ondřej Navr&#225;til

Reputation: 601

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

You forgot to close bracket of for cycle.

Upvotes: 0

Related Questions