[JS] setTimeout 또는 setInterval 함수관련 질문들
조회수 742회
var choice1 = '',choice2 = '',problem = '',p_list=[],msg='',p2_list=[],p3_list=[],n,problem2='',ans; var code=['가','나','다','라','마','바','사','아','차','카','타','파','하','자']; // 한글 조합위한 배열
function shuffle(i) { // 배열의 원소 섞는 함수
var i, t, q;
for (q = i.length; q; q -= 1) {
j = Math.floor(Math.random() * q);
t = i[q - 1];
i[q - 1] = i[j];
i[j] = t;
}
}
function word() { // 문제의 보기를 만드는 함수
for (j=1;j<=4 ;j++ )
{ choice1 = code[Math.floor(Math.random()*code.length)]; // 한글배열에서 무작위 하나 뽑기
choice2 = code[Math.floor(Math.random()*code.length)];
problem = choice1+choice2; // 뽑은 낱자를 합쳐서 단어만들기
problem2 = choice2 + choice1;
for (i=0;i<=3 ;i++ ) // 중복된 단어 걸러내기
{ while (problem == p_list[i])
{ choice1 = code[Math.floor(Math.random()*code.length)];
choice2 = code[Math.floor(Math.random()*code.length)];
problem = choice1+choice2;
if (problem != p_list[i])
{ break
}
}
}
for (i=0;i<=3 ;i++ )
{ while (problem2 == p2_list[i])
{ choice1 = code[Math.floor(Math.random()*code.length)];
choice2 = code[Math.floor(Math.random()*code.length)];
problem2 = choice2+choice1;
if (problem2 != p2_list[i])
{ break
}
}
}
p2_list.push(problem2);
p_list.push(problem); // 그 단어들을 이 배열에 네개씩 저장.
}
}
function ques() { // 문제 출제함수
x= Math.floor(Math.random()*3)
n= Math.floor(Math.random() * 3)
if (x == 0)
{ ans = eval(prompt("[유형1] \n["+p_list[n]+"] 는 몇번째 단어였을까요?",""));
if (p_list[ans-1] == p_list[n])
{ alert("정답!!");
}
else
alert("오답..");
}
else if (x == 1)
{ ans = eval(prompt("[유형2] \n["+p2_list[n]+"]는 몇번째 단어를 거꾸로 쓴것일까요?",""));
if (p2_list[ans-1] == p2_list[n])
{ alert("정답!!");
}
else
alert("오답..");
}
else if (x == 2)
{ p3_list.push(p_list[n]);
for (z=1;z<=3 ; z++)
{ word()
p3_list.push(problem)
}
shuffle(p3_list);
ans = eval(prompt("[유형3] \n다음 보기 중 아까 있었던 단어는 몇번째 단어일까요?\n["+p3_list+"]",""));
if (p3_list[ans-1] == p_list[n])
{ alert("정답!!");
}
else
alert("오답..");
}
}
function msg_reset() {
msg = '';
document.write(msg);
}
alert("멀쩡한 사람도 어려워하는 치매예방게임!");
alert("'확인' 버튼을 클릭하시면 시작됩니다!");
word()
console.log(p_list);
msg = "["+p_list+"]<br>"+"이 단어들을 잘 기억하세요!";
document.write(msg);
setTimeout(msg_reset,4000);
setTimeout(ques,4070);
코드 맨밑에 word()부터 p3_llist=[] 까지를 반복하고싶습니다.
그냥 포문같은거 쓰면 msg = "["+p_list+"]"+"이 단어들을 잘 기억하세요!";
저 부분이 안뜨고 그냥 msg_reset 상태에서 계속 머무르네요..
그리고 p_list에 생성된 단어인 problem을 push로 넣으니 반복하면 계속 배열에 원소가 쌓이기만 하는데 그걸 p_list=[] 이런식으로 하면 undefined가 뜹니다....
안배운것도 검색해가면서 해보려했는데 막힙니다 ㅠㅠ
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력