Why do i get extra undefined in last row of my output?

The_new_elixiR

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?

Scrapper142

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.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get the "extra data after last expected column' when importing .csv into my database?

Why do I get no output for my program

Why do I get "Undefined variable" in my custom PHP function

Why do I get an undefined return for my grade program?

Why do I get 'this is undefined' in my Octane component methods?

Why do I get "undefined local variable or method" in my code?

Why do I get only Bool in my output and not the string also?

Why do I get my output printed 3 times?

Why do I get this output from my while read loop?

Why do I get a null row in my sqlite database in flask?

How do I get the last row to be automatically the last populated row

How do I get my CountLines function (in Java ), to output the last number in a file?

how exactly do I get my code to output the last number of even arguments?

How do I remove these extra arrows from my output?

Why do I get an error (Notice: Undefined index) when I try to echo a value from my database?

Why do I have extra space in a grid row?

Why do I get this at the end of a `last` command?

Why do I get different row orderings when I run my query with sp_executesql?

Why do I get only the last object of an array in my Java code?

How do i get rid of an extra row created by Datagrid

Why do I get a 'constructor is undefined` error?

Why do I get property 'map' is undefined

why do i get undefined in the console in JavaScript

Why do I get undefined plus the value?

Why do I get property of undefined error?

Why do I get undefined here?

Why do I get undefined variable in an if statement?

Why do i get QJsonValue(undefined)?

Why do I have an extra attribute in my XML XElement?