Return lots of stars


Posted by Christy on 2022-04-19

Description: Write a function named makeStars that accepts a number n and return the star like below.

Note: Don’t use repeat()

makeStars(1) // *

makeStars(2) 

// *
// **

makeStars(5) 

// *
// **
// ***
// ****
// *****

The answers:

function makeStars(n) {
  let star = "";
  for (let i = 1; i <= n; i++) {
    console.log((star += "*"));
  }
  return star;
}

makeStars(5);
function makeStars(n) {
  for (let i = 1; i <= n; i++) {
    let star = "";
    for (let j = 1; j <= i; j++) {
      star += "*";
    }
    console.log(star);
  }
  return makeStars;
}

makeStars(3);









Related Posts

ASP.NET Core Web API 入門教學 - DbContext取得資料庫資料時注意事項

ASP.NET Core Web API 入門教學 - DbContext取得資料庫資料時注意事項

F2E合作社|交錯漂浮版|網頁切版

F2E合作社|交錯漂浮版|網頁切版

0o Macbook 進水沒事 o0

0o Macbook 進水沒事 o0


Comments