Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:29:50+00:00 2026-06-11T00:29:50+00:00

I am getting a undefined value for a recursive Switch Statement when the mode

  • 0

I am getting a undefined value for a recursive Switch Statement when the mode is random. The idea is that is the most id random, it would randomly select a number and set a new mode and return the original switch statement.

Everthing works fine when the mode is not in random. So I am not sure what is wrong here.
variable a is the mode, while i,j are simply numbers.

switchMode: function (i, j, a){
            var b;
            console.log(a);
            switch(a) {
                default:
                case 'add':
                    console.log(i, j);
                    b = i + j;
                    break;

                case 'minus':
                    console.log(i, j);
                    b = i - j; //negative numbers possible
                    break;

                case 'multiply':
                    console.log(i, j);
                    b = i * j; //0 possible
                    break;  

                case 'random':
                    this.randomSwitchMode(i, j);
                    break; //random
            }   
            return b;
        },



randomSwitchMode: function(i, j) {
            var c = Math.ceil(Math.random() * 3);

            console.log(i, j, c);

            switch(c) {
                default:
                case 1: 
                    var a = 'add';
                    console.log(a);
                    this.switchMode(i, j, a);
                    break;

                case 2: 
                    var a = 'minus';
                    console.log(a);
                    this.switchMode(i, j, a);
                    break;

                case 3:
                    var a = 'multiply';
                    console.log(a);
                    this.switchMode(i, j, a);
                    break; 
            } 
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:29:52+00:00Added an answer on June 11, 2026 at 12:29 am

    You’re not returning anything from “randomSwitchMode”. Even if you do that, you’ll also have to make sure you assign its return value to “b” in “switchMode”.

    Thus the random cases should look like:

                case 1: 
                    var a = 'add';
                    console.log(a);
                    return this.switchMode(i, j, a);
    

    and then in “switchMode”:

                case 'random':
                    b = this.randomSwitchMode(i, j);
                    break; //random
    

    Since you’re going to all this trouble anyway, I offer the suggestion that instead of using a switch statement you keep an object that maps operation names to functions:

    switchMode: function (i, j, a) {
      var ops = {
        'add': function(i, j) { return i + j; },
        'minus': function(i, j) { return i - j; },
        'multiply': function(i, j) { return i * j; },
        'random': function(i, j) {
          return ops[['add', 'minus', 'multiply'][Math.floor(Math.random() * 3)]](i, j);
        }
      };
      return ops[a](i, j);
    }
    

    You could split the “random” case out and handle it differently I guess. The overall point is that while there’s nothing inherently wrong with a switch statement, basing the implementation on a data structure is going to be more flexible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looping my object data but am getting this undefined value. Not sure why
I'm getting a Can't use an undefined value as a HASH reference error trying
I'm getting undefined object CreateObject(ADSystemInfo) when I embed the code in classical asp that
I am getting JQUERY undefined error. Do you have any idea (code below). <%@
I'm getting an Undefined reference error message, on this statement: GlobalClass *GlobalClass::s_instance = 0;
I am getting undefined as a value using the code below when I try
I'm getting undefined for some reason when I try to return the html via
I'm getting undefined reference when I build an application in Windows witn mingw32 (it
http://jsfiddle.net/3QeSe/44/ i am getting undefined any help appreciated ...
I have this code and I keep getting undefined if I test the selectedIndex.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.