This is my code:
function triangle1(n , shape){
let ajib =[];
for (let row = 1; row <= n; row++) {
let b ="";
let a="";
for (let space = 1; space <= n - row; space++) {
b= b + " ";
}
for (let star = 1; star <= row; star++) {
a = a + shape + " "; ///" " will make an Equilateral triangle
}
let c = b + a;
console.log(c);
ajib.push(c);
//return c ;
}
console.log(ajib)
}
console.log(triangle1(5,"a"))
This is my the output:
a
a a
a a a
a a a a
a a a a a
[ ' a ', ' a a ', ' a a a ', ' a a a a ', 'a a a a a ' ]
undefined
First: i do not know where this undefined thing come from!
How can i get rid of it?
Second: when i use return c;
instead of console.log(c)
to output each row i get a wrong result like :
a
Why does this happen and how can i solve it?
Its from console.log(triangle1(5,"a"))
because triangle has no return value. It correctly prints the letters while running the function, then undefined since the return value of triangle is undefined Second: when you use return c
you are ending the function immediately. Add it to a list and return the list
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments