Wenn ich mir den folgenden Code anschaue, wird "CheckChoice (IntSelect)" immer ausgeführt, egal, "ob (isNaN (IntSelect))" ausgeführt wird oder nicht.if-Klausel legt den Workflow in JavaScript fest?
Wenn ich ein NaN eingab und die CheckChoice-Funktion funktionierte, wurde der Alarm ausgelöst ("Der angegebene Wert liegt außerhalb des gültigen Bereichs!"); sollte auch als "Standard" Zweig angezeigt werden. Aber es passiert nicht wie erwartet.
Ich verstehe nicht, was passiert, wenn Eingang ein NaN ist, kann jemand bitte erklären.
Vielen Dank.
<!DOCTYPE html>
<html>
<head>
<title>Using the Default Clause</title>
<script language="JavaScript">
function CheckChoice(option)
{
// Make a selection.
switch (option)
{
case 1:
document.getElementById("Result").innerHTML =
"You chose Item A.";
break;
case 2:
document.getElementById("Result").innerHTML =
"You chose Item B.";
break;
case 3:
document.getElementById("Result").innerHTML =
"You chose Item C.";
break;
default:
// Display an error dialog.
alert("The value supplied is out of range!");
break;
}
}
function MakeAChoice()
{
// Ask the user to provide input.
var Selection = prompt("Type a menu option.");
// Convert the string to a number.
var IntSelect = parseInt(Selection);
// Verify the user has provided a number.
if (isNaN(IntSelect))
{
// Display an error dialog.
alert("Please provide numeric input!");
// Return without doing anything more.
return;
}
// Call the selection function.
CheckChoice(IntSelect);
}
</script>
</head>
<body>
<h1>Using the Default Clause</h1>
<p>Menu Options:</p>
<ol>
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ol>
<input type="button"
value="Choose a Menu Item"
onclick="MakeAChoice()" />
<p id="Result"></p>
</body>
</html>
Sie haben eine 'return' Anweisung im' if' Block. Weißt du was [return'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return) macht? Was ist mit dem Kommentar über dieser Aussage? –
Wenn 'isNaN' ausgeführt wird, wird' CheckChoice' zu einem nicht erreichbaren Code. – Ryan