2016-04-28 24 views

Antwort

2

Wenn wir auf Abbrechen klicken, woraufhin die Aufforderung null zurückkehren und man kann toLowerCase auf null keine Anwendung (eine Ausnahme verursachen!)

eine Bedingung hinzufügen answer===null vor allen anderen Bedingungen und return zum stoppt die Ausführung von ein function

function funcPrompt() { 
 
    var answer = prompt("Are you a photographer?", "Yes/No"); 
 
    if (answer === null || answer === "") { 
 
    alert('Please enter an answer.'); 
 
    funcPrompt(); 
 
    return; 
 
    } 
 
    answer = answer.toLowerCase(); 
 
    if (answer == "yes") { 
 
    alert('Excellent! See our links above and below to see more work and find contact info!'); 
 
    } else if (answer == "no") { 
 
    alert('That is okay! See our links above and below to learn more!'); 
 
    } else { 
 
    alert('Sorry, that answer is not an option'); 
 
    funcPrompt(); 
 
    } 
 
} 
 
funcPrompt();

0

In Ihrem Fall verwenden besser, eine confirm statt einer Eingabeaufforderung

function funcConfirm() { 
 
    var answer = confirm("Are you a photographer?"); 
 

 
    if (answer === true) { 
 
    alert('Excellent! See our links above and below to see more work and find contact info!'); 
 
    } else { 
 
    alert('That is okay! See our links above and below to learn more!'); 
 
    } 
 
} 
 

 
funcConfirm();

Verwandte Themen