2016-06-19 11 views
-2

Ich möchte raten, welche Art von Buchstaben der Benutzer abgibt.Jemand könnte erklären, was mit dem Code passiert?

var userLetter = prompt("Enter a letter and I will tell you what type of letter is","Enter here, wish me luck!"); 

function selectUserLetter(letter) { 
    var returnType = "NA"; 

    if (userLetter.charCodeAt(0) >= "A".charCodeAt(0) && userLetter.charCodeAt(0) <= "Z".charcodeAt(0)) { 
     returnType = "U"; 
    } 
    else if (userLetter.charCodeAt(0) >= "a".charCodeAt(0) && userLetter.charCodeAt(0) <= "z".charcodeAt(0)) { 
     returnType = "L"; 
    } 
    else if (userLetter.charCodeAt(0) >= "0".charCodeAt(0) && userLetter.charCodeAt(0) <= "9".charcodeAt(0)) { 
     returnType = "N"; 
    } 

    return returnType; 
} 

switch (selectUserLetter(userLetter)) { 
    case "U": 
     document.write("Your letter is Uppercase"); 
     break; 

    case "L": 
     document.write("Your letter is Lowercase"); 
     break; 

    case "N": 
     document.write("Your letter is a number"); 
     break; 

    default: 
     document.write("You typed anything else"); 
} 

Antwort

3

in Ihrem Code-Fragmente "Z".charcodeAt, "z".charcodeAt(0) und "9".charcodeAt(0) von charcodeAt Funktionsaufruf bestehen. Die Sache ist, dass JavaScript casesesitive Sprache ist. Also, charcodeAt existiert nicht eher als charCodeAt.

+0

so dumm mein Fehler ... Ich dachte, ich hätte ein Problem mit den Variablen oder dem Parameter. Ich danke dir sehr! –