Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,

I'm jumping from C# to Javascript and I'm pretty confused on the string comparison. I recieve an input from an input object with the type set to textbox and parse it like so:

if((Card1 < 1 || Card1 > 10) && (Card1.toLowerCase() !== "a" && Card1.toLowerCase() !== "j" && Card1.toLowerCase() !== "q" && Card1.toLowerCase() !== "k"))
		{
			setColor("red");
			setText("Card #1: You can only use valid cards! (1-10 and J/Q/K/A)");
			return;
		}

It's suppose to perform the action within the if function when it's not an actual play card. A play card can be any number from 1 to 10, or a Jack, Queen, King, Ace. However, right now it seems to be fine with any letter, but not with any number.

What I have tried:

Card1 < 1 || Card1 > 10) &&


to

Card1 < 1 || Card1 > 10) ||
Posted
Updated 30-Jun-17 20:55pm
v4

1 solution

Hello
I have Improved my solution:

HTML
<input type="text" id="Card1">
<button onclick="myFunction()">Click me</button>

<script>

function myFunction() {
   var a = document.getElementById("Card1").value;
   var b = parseInt(a);
   
   if(1 <= b && b <= 10) { alert("Done!"); }
   
   else {
      switch(a.toLowerCase()){
        case "a": alert("Done!");
          break;
        case "j" : alert("Done!");
          break;
        case "k": alert("Done!");
          break;
        case "q" : alert("Done!");
          break;
        default: alert("Error!");
     }
   }
   
}
</script>


I have tried it and it works fine.

Best Regards
 
Share this answer
 
v2
Comments
Member 11841791 1-Jul-17 5:56am    
Hi! This doesn't change the results. :(
Ali Majed HA 1-Jul-17 6:54am    
I have improved my answer, would you please check it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900