Lotto('this week's lucky')

정규준·2020년 4월 30일
1
lotto_function.js

// 랜덤 번호 생성 함수
function genLottoNo(){
    return Math.floor(Math.random()*45);
}

// 로또 번호 추첨 함수
function getLuckyNumber(){
    var numbers = [];
    while( numbers.length < 6 ) {
        var newNumber = genLottoNo();
        if( numbers.indexOf(newNumber)<0 ) { // 중복검사
            numbers.push(newNumber);
        }
    }
    return numbers;
}

// 11,12,13과 같은 연속된 숫자가 3개이상 나오지 않도록 [testPR]
function checkLogic(numbers) {
    for(var i=0; i<numbers.length; i++){
        var no = numbers[i];
        if( numbers.indexOf(no+1)>=0 && numbers.indexOf(no+2)>=0 ) return true;
    }
    return false;
}

function Lotto(n_lotto){
    var num_list = [];
    var numbers = getLuckyNumber();
    for (var i=0; i<n_lotto; i++) {
        var numbers = getLuckyNumber();
        while( checkLogic(numbers) ) { 
            numbers=getLuckyNumber();            
        }
        num_list.push(numbers, );
    }
    console.log(num_list);
}

module.exports.Lotto = Lotto;
lotto.js

var Lottos = require("./lotto_function");

console.log('출력할 로또 개수를 입력 부탁드려요~');

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout
});

rl.on('line', function(n_lotto) {
	Lottos.Lotto(n_lotto);
	rl.close();
});

위 코드는 lotto_function.js, lotto.js 두 파일로 나눠서 모듈 사용의 예를 보여주며 구성은 구성 함수와 실행이 되겠습니다.

위 코드 lotto.js는 다음과 같이 실행.

  $ node lotto.js
  출력할 로또 개수를 입력 부탁드려요~
  => N(입력 N 지정)

실행 결과

profile
JeongGJ__K

1개의 댓글

comment-user-thumbnail
2020년 5월 1일

( 'ㅡ')//

답글 달기