[Algorithms] 09. Steps (recursion) ๐Ÿ”ฅ

Mikyung Leeยท2021๋…„ 2์›” 9์ผ
0
post-thumbnail

๋ฌธ์ œ


ํ•ด๊ฒฐ๋ฐฉ๋ฒ•



1. from 0 to n (rows)
2. stair๋ผ๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ ๋‹ค
3. from 0 to n (colums)
4. column =< current row -> add # to 'stair'
5. else add space to 'stair'
6. console log 'stair'


  1. ์žฌ๊ท€ ํ•จ์ˆ˜ recursion - ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ž์‹ ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•˜๋Š” ๊ฒƒ.


์ œ์ถœ ์ฝ”๋“œ


function steps(n) {
  for (let row = 0; row < n; row ++) {
    let stair = '';
    
    for (let column = 0; column <n; column++) {
      if (column <= row) {
        stair += '#';
      } else {
        stair += ' ';
      }
    }
    console.log(stair)
  }
}
function steps(n, row = 0, stair = '') {
  if (n === row) {
    return;
  }
  
  if (n === stair.length) {
    console.log(stair);
    return steps(n.row+1);
  }
  if(stair.length <= row) {
    stair += '#'
  } else {
      stair += ' ';
    }
  steps (n, row, stair);
  }
function steps(n, row = 0, stair = '') {
  if (n === row) {
    return;
  }
  
  if (n === stair.length) {
    console.log(stair);  
    return steps(n.row+1);
  }
  const add = stair.length < = row ? '#' : ' ';
  steps (n, row, stair+add);
  }
profile
front-end developer ๐ŸŒท

0๊ฐœ์˜ ๋Œ“๊ธ€