심심해서 만들어본 사물함 배정 프로그램
사물함 1개당 2명씩 총 10개 사물함에 총 20명 인원을 배정

<!DOCTYPE html>
<html>
<body>
<div id="demo"></div>
<script>
const names = ["이름A", "이름B", "이름C", "이름D", "이름E", "이름F", "이름G", "이름H", "이름I", "이름J", "이름K", "이름L", "이름M", "이름N", "이름O", "이름P", "이름Q", "이름R", "이름S", "이름T"];
const lockers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let shuffledLockers = [];
shuffledLockers = shuffle(lockers);
let shuffledNames = [];
shuffledNames = shuffle(names);
let final = [];
let realFinal = [];
for(i = 0; i < lockers.length; i++) {
final.push(" 사물함" + shuffledLockers[i] + " : " + shuffledNames[i]);
    shuffledNames.splice(i,1);
}
for(i = 0; i < lockers.length; i++) {
realFinal.push(final[i] + " , " + shuffledNames[i] + "\n");
}
alert(realFinal);
function shuffle(array) {
  let currentIndex = array.length,  randomIndex;
  while (currentIndex != 0) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;
    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]];
  }
  return array;
}
</script>
</body>
</html>

댓글

이 블로그의 인기 게시물

Java InetAddress.getHostName()/getHostAddress

Scanner 클래스로 -1이 입력될 때까지 양의 정수를 입력 받아 저장(List객체)하고 검색하여 가장 큰 수를 출력하는 프로그램을 작성하라.

Text editing shortcuts that work on eclipse