Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
How do I make this JavaScript console.log into 6 rows with 6 indexes?

Instead of 1 index on each row, I want horizontal not vertical returns.

let n = [6];

function staircase() {
  for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= n - i; j++) {
      console.log("0");
    }
    for (let k = 1; k <= i; k++) {
      console.log('#');
    }

  }
}

staircase();

But I need it to render like this. (This works using Java; it's the same logic, but renders differently.)

     #
    ##
   ###
  ####
 #####
######
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

console.log prints one line each therefore i put 2 variables to keep the empty string and hash string and after the loops logged the concatenation of the strings

let n = 6

function staircase() {
  for (let i = 1; i <= n; i++) {
    let emptyString = '';
    let hashString = '';
    for (let j = 1; j <= n - i; j++) {
      emptyString += " ";
    }
    for (let k = 1; k <= i; k++) {
      hashString += '#'
    }
    console.log(emptyString + hashString)

  }
}

staircase();

about 4 years ago · Juan Pablo Isaza Report

0

const n = [6] 
for (let i = 1; i <= n; i++) {
     //
    // this array stores the results; 
   //
    const array = [];
  //
    for (let j = 1; j <= n - i; j++) {
      array.push("0");
    }
    for (let k = 1; k <= i; k++) {
      array.push('#');
    }
    // adding `toString`, does the magic
    console.log(array.toString())
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!