[RESOLVED]What's wrong with this function?

I am getting "my" is undefined.

<head>
    <title></title>
    <script type="text/javascript">
        var refButton = document.getElementById("btnButton");
        function my() {
        var c = Math.floor(Math.random());
        switch (c)
        {
        var m;
        1: m=’January’;
           break;
        2: m=’February’;
           break;       
        3: m=’March’;
           break;
        4: m=’April’;
           break;
        5: m=’May’;
           break;
        6: m=’June’;
           break;
        7: m=’July’;
           break;
        8: m=’August’;
           break;
        9: m=’September’;
           break;
        10: m=’October’;
           break;
        11: m=’November’;
           break;
        12: m=’December’;
           break;
           }
           alert(m);
           });

     
    </script>
</head>
<body>
    <form id="form1">
    <div>
        <input id="btnButton" type="button" onclick="my()" value="Click me"/>
    </div>
    </form>
</body>
</html>

You are declaring the variable inside switch statement and then try to access it outside the switch statement . When you declare it inside the switch statement the scope of variable is limited to switch statement.You need to declare the variable outside
of switch statement.

Also the code which you use to generate random digit will always generate 0 which is not a valid value in your case. I have added a new function which will generate random numbers between 1 and 12.

You can try with the below code

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        var refButton = document.getElementById("btnButton");
        function my() {
          //Declare the variable here outside of switch stagement
          var m;
          //Generate the Random integer values between 1 and 12
          var c = getRandomInt(1,12);
          
         switch (c)
        {
          
          case 1: 
            m='January';
             break;
         case 2: m='February';
             break;       
         case 3: m='March';
             break;
         case 4: m='April';
             break;
        case 5: m='May';
             break;
          case 6: m='June';
             break;
          case 7: m='July';
             break;
          case 8: m='August';
             break;
          case 9: m='September';
             break;
          case 10: m='October';
             break;
         case 11: m='November';
             break;
          case 12: m='December';
             break;
           }
           alert(m);
           }
      //Function to generate the randon number
      function getRandomInt(min, max) {
          return Math.floor(Math.random() * (max - min + 1)) + min;
      }
    </script>
</head>
<body>
    <form id="form1">
    <div>
        <input id="btnButton" type="button" onclick="my()" value="Click me"/>
    </div>
    </form>
</body>
</html>

Hi davelewicki,

Thanks for your post.

davelewicki

<script type="text/javascript">
        var refButton = document.getElementById("btnButton");
        function my() {
        var c = Math.floor(Math.random());
        switch (c)
        {
        var m;
        1: m=’January’;
           break;
        2: m=’February’;
           break;       
        3: m=’March’;
           break;
        4: m=’April’;
           break;
        5: m=’May’;
           break;
        6: m=’June’;
           break;
        7: m=’July’;
           break;
        8: m=’August’;
           break;
        9: m=’September’;
           break;
        10: m=’October’;
           break;
        11: m=’November’;
           break;
        12: m=’December’;
           break;
           }
           alert(m);
           });
     </script>

According to your code, we could find that the JavaScript switch statement causes a Syntax error, so you get the ‘my’ is undefined error. The correct JavaScript switch statement looks like below.

switch(expression) {
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
    default code block
}

You could use A2H’s solution to resolve your problem. Besides, you could refer to the following link to learn more about JavaScript switch statement.

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply