Thursday, May 20, 2021

Functional switch statement in Javascript

Say I have a switch statement that takes a variable myData between 1 and some larger value, i.e. 300.


Depending on the "status code" variable, the result is the text value of another variable. For example, if myData == 1, I want to return the a variable called code1. If myData == 300, I want to return the a variable called code300. Code1 and code300 variables store unrelated strings, i.e. "This is a summary", or "This is a note". Some psuedo-code below:


var myData = statusCode;


var code1 = "This is a summary";

var code300 = "This is a note";


switch(myData) {

    case statusCode:

        scriptletResult = returnCode("code", statusCode); // code1 if myData == 1

        break;          

    default:

        scriptletResult = code1;

}


function returnCode(code, statusCode) {

    return code + statusCode; // Returns a variable "code1" if statusCode == 1

}

How can I get this to work?

No comments:

Post a Comment

Features of the JS language

 Features of the JS language The main features of this programming language are: Dynamic typing. That is, the data type will only be determi...