Javascript String check char type

Description

Javascript String check char type


function checkCharType(charToCheck) {
    let returnValue = "O";
    let charCode = charToCheck.charCodeAt(0);

    if (charCode >= "A".charCodeAt(0) && charCode <= "Z".charCodeAt(0)) {
        returnValue = "U";
    } else if (charCode >= "a".charCodeAt(0) &&
               charCode <= "z".charCodeAt(0)) {
        returnValue = "L";
    } else if (charCode >= "0".charCodeAt(0) &&
               charCode <= "9".charCodeAt(0)) {
        returnValue = "N";
    }// w  w w  .  j a v a 2 s  .  c  o m

    return returnValue;
}

let myString = "Hello World!";

switch (checkCharType(myString)) {
    case "U":
        console.log("First character was upper case");
        break;
    case "L":
        console.log("First character was lower case");
        break;
    case "N":
        console.log("First character was a number");
        break;
    default:
        console.log("First character was not a character or a number");
}



PreviousNext

Related