I am very beginner, when I input 1 or 2 it prints default value. same thing for Upper Case "A". any body can correct my code. Thank you

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript switch</h2>

<input id = "input1" type = "text"/><br>

<button onclick ="myFunction()">Click me </button>

<p id="demo"></p>

<script>
function myFunction(){

let check = input1.value;

let txt;
switch (check) {
  case 1:
    txt = "Off";
    break;
  case 2:
    txt = "On";
    break;
    case "a":
    txt = "Hello This is 'a' But problem with 'A'"
    break;
  default:
    txt = "No value found";
  }
document.getElementById("demo").innerHTML = txt;


}

</script>

</body>
</html>

Do you have a question…?

I am very beginner, when I input 1 or 2 it prints default value. same thing for Upper Case β€œA”. any body can correct my code. Thank you

A few things here.

Take a close look here

You told the computer that this is an input of text.
That means the value you are getting from it is a string.

When you type in 1 for the input which is a string that is not the same as case 1 which is a number.
Those are two different types.

You can see for yourself the type of the input if you add a console.log

  let check = input1.value;

      console.log(typeof check)

If you want the first two cases to execute, then you need to change it to case β€œ1” and case β€œ2”.

Hope that helps!

1 Like

I created two codepen examples so you can see it working.

The first example, works with strings.
We can test case β€œ1” and case β€œ2”.
I also added another case to test for upper case A since that was one of your questions.

When you enter in 1 or 2 for the first example, you should get the correct input.
Screen Shot 2021-09-09 at 7.15.37 PM

The second example, is with numbers.
If you want to test case 1 and case 2, you have to convert that string input into a number using parseInt

Hope that helps!

1 Like

Thank You very much, but I would like to both string and number together. for example
If input letters like β€œa, or b, or c” it will print out string β€œa or b or c”. If input 1 or 2 or 3, it will print out number 1, or 2, or 3. in the same input box. can you help me out
thank you again.

Yeah you can do that.

I would suggest first getting the value of the user input and assigning it to a variable.
Then you can write a condition that checks if the string input contains numbers.
If so, covert that string input into a number.

Then you can create your switch statement checking if the user input matches any of the cases.

Give that a try and if you need help, show us your code and we can go from there.

1 Like

Hi there,
Thank you for your co-operation. I am a self-thought learner. I have a Q, How much JavaScript do I need to learn for frontEnd development? Do you think 111 lessons on the FCC website will be enough for every beginner ? can you please send a guideline to start front-end dev? Specially javaScript because JS is used both frontend and backend. but I would like to learn JS for the front end now and for the backend later. so how much JS do I need to learn?
Thank you

I would say the minimum is to complete the whole javascript certification and the front end certification.

Then from there you keep learning by building your own projects to strengthen your skills.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.