배열을 이용해서 별찍기

안시우·2024년 4월 30일

자바스크립트

목록 보기
3/5

1. 설명

  • 배열 만들기

2. 코드

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>배열로 만들어서 별표찍기</title>
</head>
<body>
  <h1>배열로 만들어서 별표찍기</h1>
  <hr>
  <script>
  	const NUMBER_COUNT = 6;
  	//배열 만들기
    const numArray = Array();
    //배열에 무작위 숫자 넣기
    for(let i=0;i<NUMBER_COUNT;i++){
      numArray[i] = parseInt(Math.random()*(30-5+1)+5);
    }
    //배열 출력하기
    document.write(`const numArray = [`);
    for(let i=0;i<numArray.length;i++){
      i === numArray.length-1 ? (document.write(`${numArray[i]}`)) : (document.write(`${numArray[i]} , `));
    }
    document.write(`] <br>`);
	//배열에서 값을 불러와서 별 찍기
    for(let i=0;i<numArray.length;i++){
      let value = numArray[i];
      for(let j=0;j<value;j++){
        document.write(`*`);
      }
      document.write(` ${value} <br>`);
    }
  </script>
</body>
</html>

3. 결과

0개의 댓글