jeudi 24 novembre 2022

Printing Triangle star(*) pattern in javascript using Recursion [duplicate]

I have been trying to solve printing down left side star(*) pattern in Javascript using recursion, i think my logic is correct but my syntax and concept might be wrong

// * * * * *
// * * * *
// * * *
// * *
// *

This is my code solution so far

var triangle = function (row, col) {
    if(row == 0){
        return
    }
    if(col < row){
        console.log("*")
        triangle(row, col + 1)
    }else{
        console.log("\n")
        triangle(row - 1, 0)
    }
}
triangle(4, 0)

output

*
*
*
*


*
*
*


*
*


*

But i want the output to be

* * * * *
* * * *
* * *
* *
*

Aucun commentaire:

Enregistrer un commentaire