nodejs 과제 겸으로 로또 프로그램을 작성해보았습니다.
/**
* top.js, main()
*/
"use strict";
const lotto = require('./lotto');
const print = text => console.log(text);
const lottoNum = lotto.run();
let index = 0;
print(`\n┌─☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ─┐\n│`);
lottoNum.forEach((data)=>{
print(`│ ${++index} 번째 번호 : ${data}`);
for(let i = 0; ++i < 2000000000;); // show delay
});
print(`│\n└─※ 당첨 번호 : [ ${lottoNum} ]`)
/**
* lotto.js
*/
"use strict";
const { random } = require('./random');
const DATA_NUM = [1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,
40,41,42,43,44,45];
const BALL_MAX = 6;
/**
* return -> [], length : 6
*/
const run = () => {
let numArray = DATA_NUM;
let result = [];
for(let i = -1; ++i < BALL_MAX;){
let num = random(numArray.length);
result[i] = numArray[num];
numArray.splice(num, 1);
}
return result;
}
module.exports = {
run,
}
/**
* random.js
*/
"use strict";
const date = new Date();
const seed = Number.parseInt(`${date.getMilliseconds()}${date.getHours()}${date.getMinutes()}${date.getFullYear()}${date.getSeconds()}${date.getMonth()}`);
/**
* range -> not 0
* return -> 0 ~ range - 1
*/
const random = (range) => {
return ((Math.floor(Math.random() * range) + 1) ^ seed >>> 1) % range;
}
module.exports = {
random,
}