2017-12-03 54 views
0

저는 디지털 아트 학생입니다. 저는 1 학년이며 최근에는 수학을위한 기본적인 전자 학습 웹 사이트를 만들 수있는 임무를 부여 받았습니다. 기본적으로 (예를 들어) 7 번 7 번이 무엇인지 묻고 답을 입력하고 버튼을 클릭하면 경고 창이 나타납니다. 당신이 옳다면 "정확함"또는 "예"또는 "잘 끝났습니다"라고 말하며, 틀리면 "아니오"또는 "다시 시도"또는 다른 것을 말합니다.window.alert (자바 스크립트)에 랜덤 메시지 지정

내 문제는 하나의 임의의 메시지를 창 경고에 표시하는 것으로 가정합니다. 한 번에 하나씩 또는 하나씩 무작위로 하나씩 표시하는 것으로되어 있습니다. 나는 보았지만 나는 도움이되는 것을 찾을 수 없었다. 대신

window.alert("Correct"); 
    window.alert("Yes!"); 
    window.alert("Well done"); 

당신이 :이 같은 동시에 모든 메시지를 작성하는 것을

<script type="text/javascript"> 
    var x, y; 
    var answer; 



    function aNumber() { 

     return Math.floor(1 + (Math.random() * 12)); 

    } 

    function genQuestion() { 
     x = aNumber(); 
     y = aNumber(); 
     din = document.getElementById("inputVal"); 
     din.value = x + " times " + y; 
     answer = x * y; 
    } 



    function ClickAnswer() { 
     if (answer == document.getElementById("outputVal").value) { 
      window.alert("Correct"); 
      window.alert("Yes!"); 
      window.alert("Well done"); 
      location.reload(); 

     } else { 
      window.alert("No, try again."); 
      window.alert("try again"); 
      window.alert("wrong"); 
     } 

    } 

    function displayArea() { 
     din = document.getElementById("inputVal"); 
     dout = document.getElementById("outputVal"); 
     dout.value = circleArea(din.value); 
    } 

</script> 

답변

0

문제입니다 : 여기

내가에 문제가있어 코드의 일부 메시지를 배열에 저장하고 임의의 숫자를 선택하여이 배열에서 메시지를 선택할 수 있습니다. 당신은 이런 식으로 뭔가를 시도 할 수 있습니다 :

var message = ["Correct","Yes!","Well done"]; 
 

 
var a = Math.floor(Math.random() * message.length); 
 

 
window.alert(message[a]);

+0

수 있습니다. 사용해야합니다 message.length – webdevdani

+0

@webdevdani 예, 내가 그것을 바 꾸었습니다 .. 내가 할 수있는 방법에 집중했다;) –

3

배열에 메시지를 넣고 그것의 임의의 항목을 알려줍니다.

function ClickAnswer() { 
    if (answer == document.getElementById("outputVal").value) { 
     alert(successMsg[Math.floor(Math.random() * successMsg.length)]); 

     location.reload(); 

    } else { 
     alert(errorMsg[Math.floor(Math.random() * successMsg.length)]); 
    } 
0

let successMsg = ['Correct', 'Cool']; 
 
let errorMsg = ['Wrong', 'false']; 
 

 
alert(successMsg[Math.floor(Math.random() * successMsg.length)]);
은 당신이 할 방법을 찾고하면 숫자 선택에 그랬던 것처럼 Math.random()의 활용이다. 하나의 배열에 대한 각각의 정답과 다른 배열에 대한 각각의 오답을 할당하십시오. 그럼 당신은 그들을 검색 할 수 있습니다

correct_answers[Math.floor(Math.random()*correct_answers.length)]; 
imcorrect_answers[Math.floor(Math.random()*incorrect_answers.length)]; 

내가 원래 코드 제거 다운 버전을 만든이 여기 소개 :

var correct_answers = ["Correct", "Yes!", "Well done"]; 
 
var incorrecty_answers = ["No, try again.", "try again", "wrong"]; 
 

 
correct_answers[Math.floor(Math.random()*correct_answers.length)]; 
 

 
function ClickAnswer() { 
 
    if (true) { 
 
    window.alert(correct_answers[Math.floor(Math.random()*correct_answers.length)]); 
 
    } else { 
 
    window.alert(incorrect_answers[Math.floor(Math.random()*incorrect_answers.length)]); 
 
    } 
 
} 
 

 
ClickAnswer();

희망이 도움이! :)

0

배열에 임의화할 문자열 값을 저장하고 무작위로 특정 인덱스를 선택하여 반환 할 수 있습니다. 보세요 here. 귀하의 경우에는

는 길이가 매우 관리가 아닌 하드 코딩

var positiveAnswers= ['Correct!', 'Yes!', 'Well done!'];  
var randomAnswer = positiveAnswers[Math.floor(Math.random() * positiveAnswers.length)]; 

window.alert(randomAswer);