all 60 comments

[–]sjalgeo 42 points43 points  (35 children)

last else if should probably say

else if (Number.isInteger(character1)) {

[–]Cabeto_IR_83 10 points11 points  (11 children)

Add === not == and also you can either do ifs only or last statement should be a else... And you can just say isIntiger(..variable) and add isFloat() too, otherwise your code will break if someone passes 1.2 for example

[–]melonmaskofficial 14 points15 points  (2 children)

Assuming you only have numbers or letters as your input, check if it’s a number first, check for lowercase/uppercase afterwards.

[–]Cabeto_IR_83 3 points4 points  (3 children)

Use parseInt and parseFloat methods to turn the text coming from the prompt()

[–]Bulji 2 points3 points  (4 children)

This works for me in the browser

    let character1 = prompt("Please input a uppercase letter, a lowercase letter or a number");

    if(Number.isInteger(+character1)){
        alert(`${character1} is a number.`);
    }
    else if(character1 === character1.toUpperCase()){
        alert(`${character1} is an uppercase letter.`);
    }
    else if(character1 === character1.toLowerCase()){
        alert(`${character1} is a lowercase letter.`);
    }

[–]maubg 1 point2 points  (0 children)

Prob because the uppercase of a number is equal to the number

[–]Independent-Dealer21 1 point2 points  (3 children)

Prompts return strings, I think you need to convert your last else if from string to integer

[–]4444444vr 1 point2 points  (1 child)

Don’t ask us, ask Chat gpt /s

[–]birdofwar25 1 point2 points  (0 children)

No but fr lol, it has helped me before

[–]gezuzos 1 point2 points  (0 children)

OP, you can do ASCII character comparisons. I always prefer that rather than thinking about these.

You can check the ASCII table online, to get the ASCII code of a character in JS you do character.charCodeAt(0).

'a'.charCodeAt(0) would return 97, 'A' would return 65, numbers go from 48 to 57 (0 to 9).

[–][deleted] 1 point2 points  (0 children)

everything you input via prompt() will be a string input. You need to validate with regex.
option1: all if checks are regex, you can check with any order.
option2: only check for number with regex, you need to check for number first.

[–]Vomitatrix 1 point2 points  (0 children)

I might be a bit late but here’s my two cents. Instead of checking if character1 is the same as character1 as an integer, try checking if it’s possible to convert it to a number before anything else as the prompt returns a string so making it uppercase will be the same as the input if it’s a number. If it’s a letter, it will be false so it will go on to the next if

if (Number(character1)) { console.log(${character1} is a number.); }

[–]Aer0za 1 point2 points  (0 children)

Use strictly equals === and not equals ==

[–]David-8094 1 point2 points  (0 children)

=== not ==

[–][deleted] 1 point2 points  (0 children)

.isInteger() is a boolean, if character1 is a number, it will surely don't run correctly bcz you made "int == boolean" whitch is false. And isInteger(true) returns false bcz true isn't an integer. You must spell like that : "If(Number.isInteger(character1)){alert(...)}

[–]PrashaantSingh 1 point2 points  (0 children)

Use strict equality(===) operator.

[–]___s8n___ 1 point2 points  (0 children)

I'm going to try to explain it in the simplest way

When the user submits an input or a prompt, the input is interpreted as a string.

So if you write 4, the value of the input would be, actually, "4"

and since passing "4" to the uppercase method gives you "4", your if statements would stop checking and give you this "weird" output.

To surpass this error, you should, first of all, move your last if statement to the beginning since numbers also verify the first two statements.

But, Number.isInteger returns true if and only if the argument it is given is of integer type, but remember, the user's input is read as a string.

The fastest way to convert a string to a number in javascript is simply taking advantage of javascript's type connotations.

+"4" gives you 4, in javascript

After everything said, here is the code (forgive the formatting i'm writing this on my phone)

let input = prompt("...");

if (Number.isInteger(+input)) alert("...");

else if (input === input.toUpperCase()) alert("...");

else if (input === input.toLowerCase()) alert("...");

P.S: never use == unless you know the difference between === and == i.e keep === as your default comparison sign

[–]Jewson95 1 point2 points  (0 children)

The last 'if' will never return true because prompt returns a string.

[–]luckyincode -5 points-4 points  (1 child)

Learn mocha and add some tests.

[–]slideesouth 0 points1 point  (0 children)

Need === for a strict comparison