How to display pyramid using JavaScript?

前端 未结 22 831
长情又很酷
长情又很酷 2020-11-29 11:45

Here is the code to display pyramid but its not exactly producing required output.

相关标签:
22条回答
  • 2020-11-29 12:04

    The easiest solution is:-

    function pyramid(n) {
    
     
    
      for(let i=1; i<= n; i++){
    
        let str = ' '.repeat(n-i);
    
        let str2 = '*'. repeat(i*2 -1)
    
     
    
        console.log(str + str2 + str);
    
      }
    
    }
    
    pyramid(5);

    0 讨论(0)
  • 2020-11-29 12:08

    This could be done using a single for loop.

    var num = "";
    var size = prompt("Enter the size of the pyramid");
    for(var i=1; i<=size; i++)
    {
      num = num + i
      console.log(num);
    }

    0 讨论(0)
  • 2020-11-29 12:10

    If we're talking about the 'pyramid' problem, then this would be an appropriate solution.

    function pyramid(n) { // If (e.g.) n=3;
      const columnLength = (n * 2) - 1; // 5 
      let middle = Math.floor(columnLength / 2) // middle would be 2
      for(let row=0; row<n; row++) { // let's create the rows (row = horizontal)
        let res = ''; // init our output inside of the 1st for loop
        for(let col=0; col<columnLength; col++) { // creating the columns (column = vertical)
    // The following formula would yield the result we need: 
    // (n * 2) - 1 => row=2;col=3; row=3;col=5; row=5;col=9
    // We got 2 sides, right? 
    // So, before we insert '#' we need to make sure the following logic is met:
    // Look at:  (middle - row) as being the left side and  (middle + row) as the right one.
    // Only if both conditions are met, we want to insert the "#" sign
          if(middle - row <= col && middle + row >= col ) {
             res += '#';
          } else {
    // If '#' is NOT inserted then we want to insert something else, right?! 
    // In our case that would be an empty string
            res += ' ';
          }
        }
        console.log(res);
      }
    }
    
    pyramid(3);


    And if we want to be extra 'fancy, we coučld implement recursion:

    function recursiveP(n, row=0, res='') { // IMPORTANT: Pass some default values
          const columnLength = (n * 2) - 1;
          let middle = Math.floor(columnLength / 2);
          // This is our EXIT condition, meaning, if have the n number of rows, our work is done!!
          if(n === row) {
            return;
          }
          //  *** Moving on ***
          //   Initially, this will be skipped, and we'll go to the next check and add the appropriate character,
          //   however, after we're finished w/ creating the 1st row we'll hit this check, we'll print the previously generated result,
          //   and call the function again, but this time incrementing the ROW value. This will continue until the 1st check is met
          if(res.length === columnLength) {
             console.log(res);
             return recursiveP(n, row + 1);
          }
          //   Here we're creating the columns and in each, we're inserting the appropriate char
          if(middle - row <= res.length && middle + row >= res.length ) {
             res += '#';
          } else {
             res += ' ';
          }
          //Initial [recursive] function call
          recursiveP(n, row, res);
        }
        
        recursiveP(6);

    0 讨论(0)
  • 2020-11-29 12:12

    function pyramid(n){ const midpoint = Math.floor((2 * n-1)/2);

    for(let row = 0 ; row < n ; row ++){ let level = '';

     for(let column = 0 ; column < 2*n-1 ; column++)
        {
         if(midpoint-row <= column && midpoint + row >= 
            column){
            level += '#';
           }
           else{
               level += ' ';
               }
    
        }
       console.log(level);
      }
    

    }

    pyramid(5);

    0 讨论(0)
  • 2020-11-29 12:13
      const pyramid = (n)=>{
      const mid = Math.floor((2*n-1)/2);
      for(let row=0; row<n; ++row)
      {
        //for each row, make empty steps
        let level = ''
        for(let col=0; col<2*n-1; col++)
        {
          if(mid-row <=col && mid+row >= col)
            level+='#';
          else level +=' ';
        }
        console.log(level);
      }
    }
    pyramid(3);
    
    0 讨论(0)
  • 2020-11-29 12:13

    To draw a pyramid on the console using JavaScript

    1. Make each line have an odd number of fill characters.
    2. Prepend spaces (or 'spacer characters') before each line, excluding the last.
      To do this:
      • Use repeat() to determine the number of spacer characters for each line. You do that by passing the number of lines - 1 as an argument.

    Here's my solution

    function drawPyramid(lines, fillChar, spacerChar) {
      let fillChars = '';
      let spacer = spacerChar || ' '; // Default spacer is ' '
      let spacerCount = lines;
    
      for (let i = 1; i <= lines; i++) {
          fillChars += fillChar;
    
      // Makes lines always have an odd number of fill characters
        if (i >= 2)
            fillChars = fillChar + fillChars;
    
        console.log(spacer.repeat(spacerCount - 1) + fillChars);
        spacerCount--;
      }
    }
    
    drawPyramid(4, '*');

    0 讨论(0)
提交回复
热议问题