Better way than using multiple if - javascript

I have a simple problem: I need to roll two sets of dice. For each number possible in the first roll, there are a set of conditionals that I then check against the second roll. The conditionals for each roll are different.
The problem, to me, is that something like this repeated roughly thirty times seems like a gross thing to stick in my code:
if (roll == 3) {
if (secondRoll < 5) {
do this
}
else if (secondRoll > 5 && secondRoll < 10) {
do this
}
else {
do this
}
}
if ...... and so on
So what do I do? Is there a more elegant way to do this? I've thought about something like this:
class Result {
constructor(firstRoll, secondRollMin, secondRollMax, output) {
this.firstRoll;
this.secondRollMin;
this.secondRollMax;
this.output;
}
}
and then checking the rolls against matching properties in a set of objects, but I'm not sure that's honestly any better. Any thoughts would be appreciated.

Just a small improvement, but you only need to check the upper bound of the range in each subsequence else if:
if (roll == 3) {
if (secondRoll < 5) {
do this
}
else if (secondRoll < 10) { // secondRoll must be >= 5 already
do this
}
else {
do this
}
}

Use Switch to simplify
switch (roll) {
case 1-4:
// Do something.
break;
case 5-8:
// Do something.
break;
case 9-11:
// Do something.
break;
default:
break;
}

How about creating a key for the combination and a bit simpler if/else? You could combine any combination that has the same action.
const combo = `${roll}-${secondRoll}`;
if (['1-1', '1-2', '1-3', '3-4', '3-5', '3-6'].includes(combo) {
// Do this
} else if (['1-4', '1-5', '1-6', '3-1', '3-2', '3-3'].includes(combo) {
// Do this
// ...
} else if (['6-4', '6-5', '6-6'].includes(combo) {
// Do this
}
Or create a switch/case:
const combo = `${roll}-${secondRoll}`;
switch (combo) {
case '1-1':
case '1-2':
case '1-3':
case '3-4':
case '4-5':
case '5-6':
// Do this
break;
case '1-4':
case '1-5':
case '1-6':
case '3-1':
case '4-2':
case '5-3':
// Do this
break;
// ...
case '6-4':
case '6-5':
case '6-6':
// Do this
break;
}

You can try a mixture of if, else and switch blocks. You can use different methods to call based on the first roll. A switch statement is usually more efficient than a set of nested ifs.
class Result {
constructor(firstRoll, secondRollMin, secondRollMax, output) {
this.firstRoll;
this.secondRollMin;
this.secondRollMax;
this.output;
switch(firstRoll){
case 1:
this.output = oneToXTime(secondRollMin,secondRollMin)
break;
case 2:
this.output = twoToXTime(secondRollMin,secondRollMax)
break;
case 3:
this.output = threeToXTime(secondRollMin,secondRollMax)
break;
case 4:
this.output = fourToXTime(secondRollMin,secondRollMax)
break;
case 5:
this.output = fiveToXTime(secondRollMin,secondRollMax)
break;
case 6:
this.output = sixToXTime(secondRollMin,secondRollMax)
break;
}
}
static String oneToXTime(secondRollMin,secondRollMin) {
String result = ""
if (secondRollMin < 5) {
result = "My result"
}
else if (secondRollMin < 10) {
result = "My result 2"
}
return result
}
static String twoToXTime(secondRollMin,secondRollMin) {
String result = ""
if (secondRollMin < 5) {
result = "My result"
}
else if (secondRollMin < 10) {
result = "My result 2"
}
return result
}
static String threeToXTime(secondRollMin,secondRollMin) {
// Same as above...
}
static Stfing fourToXTime(secondRollMin,secondRollMin) {
// Same as above...
}
static String fiveToXTime(secondRollMin,secondRollMin) {
// Same as above...
}
static String sixToXTime(secondRollMin,secondRollMin) {
// Same as above...
}
}

Related

How do i make switch and if statement in one function

function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
var answer = "low";
case 4:
case 5:
case 6:
var answer = "medium";
break;
} else if(val => 7) {
var answer = "Huge"
}
return answer;
}
it says error Declaration or statement expected. ts(1128) [13, 7]
and it poits at the else if statement
You can use the "default" keyword, but you should probably update your code in order to handle the cases in which the value of the parameter is not positive or not a number:
function texas(val) {
if (val <= 0 || isNan(val)) {
throw new InvalidOperationException("val should be a positive number");
}
switch(val) {
case 1:
case 2:
case 3:
return "low";
case 4:
case 5:
case 6:
return "medium";
default:
return "Huge"
}
}
It's >= and the elsehas to be deleted. The varfor answer is unnecesary, just declare it once with let. You forgot the break in case 3:.
function texas(val) {
let answer = "";
switch(val) {
case 1:
case 2:
case 3:
answer = "low";
break;
case 4:
case 5:
case 6:
answer = "medium";
break;
}
if(val >= 7) {
answer = "Huge"
}
return answer;
}
console.log(texas(2));
console.log(texas(8));
You just need to return in the switch
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
var answer = "low";
return answer;
case 4:
case 5:
case 6:
var answer = "medium";
return answer;
}
if(val => 7) {
var answer = "Huge"
}
return answer;
}
The syntax does not allow to put an else after a switch. else only makes sense in combination with an if statemen. But switch has a default: case which most closely matches your intention (hopefully):
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
var answer = "low";
break;
case 4:
case 5:
case 6:
answer = "medium";
break;
default:
if(val >= 7) {
answer = "Huge"
}
// decide what should happen if val is 0, -1 or not even a number (e.g. texas('gotcha!')
break;
}
return answer;
}
Don't forget to put break in your cases, otherwise execution will "fall through" and execute the next cases. You would never end up with "low"
You can't use an if statement within a switch block.
You do have the default option tho -
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
answer = "low";
case 4:
case 5:
case 6:
answer = "medium";
break;
default:
answer = val >= 7 ? "Huge" : "Invalid";
break;
}
return answer;
Note that if you have a minus / negative answer, it'll also fall into this clause, but you can the the value of answer with an inline ?: if statement...
You can't put the else after the switch block as people have stated above. switch statement is better for multi way branching and fixed data values. On the other side, if statement is better for boolean values. You can do something like this. It might not be the shortest line of codes, but just so you that there's another approach:
function texas(val) {
let answer = "";
switch (true) {
case (val == 1 || val == 2 || val == 3):
answer = "low";
break;
case (val == 4 || val == 5 || val == 6):
answer = "medium";
break;
case (val >= 7):
answer = "huge";
break;
}
return answer;
}

Why "if" works but "switch" doesn't work?

// It is simple code
var num = prompt("put number");
// This way is not worked
switch (num) {
case num > 0:
console.log("num++");
break;
case num < 0:
console.log(num-2);
break;
}
// But this worked
if (num > 0){
console.log(num++);
} else if (num < 0){
console.log(num -2);
}
My first way by "switch" is not worked but "if" method worked.
I tried all of thing for changing code or other ways but the same result.
Please guys help me.
Because the statement num > 0 inside you case will return true or false.
If you do this:
switch (true) {
case num > 0:
console.log("num++");
break;
case num < 0:
console.log(num-2);
break;
}
It will work.
Cases cannot be expressions, you must normalize your input first.
Although it is valid to place an expression in a case, in this scenario a more tried-and-true way of dealing with this is to first normalize your input first.
You can determine direction for example:
var num = parseInt(prompt("put number"), 10);
var direction = num < 0 ? -1 : 1;
switch (direction) {
case 1:
console.log("num++");
break;
case -1:
console.log(num - 2);
break;
}
The switch acts as a case switcher, meaning you cannot make comparisons to create cases, just list cases by case, and perform some function from this case. The if / else structure is suitable for making comparisons, as the expected result in the if call is always a boolean.
Example:
const a = 1;
if (a === 1) {
console.log('hello');
} else {
console.log('sad');
switch (a) {
case 1 : console.log('hello'); break;
default: console.log('sad'); break;
In your case, I recommend using if/else if/else, as it is more recommended.

switch case of greater than or less than variable [duplicate]

This question already has answers here:
Expression inside switch case statement
(8 answers)
Closed 3 years ago.
I use this code to enter a number and compare it using less and greater than within switch case how should I do to get the correct result, only default can works
var sum=prompt("enter sum:");
// sum=50;
switch(sum)
{
case sum=0:
alert("sucess");
break;
case sum>50:
alert("also sucess");
break;
case sum<50:
alert("failed");
default:
alert("there is errorrrr");
}
You can use switch (true):
switch (true) {
case sum === 0:
alert('success');
break;
case sum < 50:
alert('also success');
break;
case sum > 50:
alert('failed');
break;
default:
alert('there is an error.')
break;
}
Note that in your code, the first case is actually an assignment and modify sum to set it to 0.
It actually doesn't work, as you expect, the switch statement is compared against all cases, such as :
switch (something)
{
case 1: // something == 1 ?
// ....
}
Actually, what you have write was interpreted such as
var sum = 42;
switch(sum)
{
case sum < 50: // sum == sum < 50 ? -> 42 == 42 < 50 ? -> 42 == true ? false !
// ...
Instead, you can use a switch true statement.
// v------- convert the prompt to a numeric value
let sum = + prompt("enter sum:");
switch(true)
{
// VV----- notice the double equal
case sum == 0: // true == sum == 0 ?
alert("sucess");
break;
case sum > 50:
alert("also sucess");
break;
case sum < 50:
alert("failed");
break; // <---- You forgot a break; there
default:
alert("there is errorrrr");
break;
}
ur idea works fine
sum=20;
switch(true)
{
case 50:
alert("sucess");
break;
case (sum>50):
alert("also sucess");
break;
case sum<50:
alert("failed");
break;
default:
alert("there is errorrrr");
}

How do I convert the following to if statements?

So I play a RPG on a Virtual Tabletop that supports API. I do not want to create a bunch of objects individually, so I am working with the API, and want to read attributes from a JSON dump and then write those attributes to a object in the game (Character). So all goes well with my code so far as long as I am pulling static info. But in the case of Skills, they may or may not have all of the attr defined, so NULL. I would like to be able to identifiy the null and move on, not fail out because it is NULL.
I have attached a GIST, I am a n00b to this, and I am a System Engineer in real life, but not a coder. So I would appreciate the input!
https://gist.github.com/bigdadmike/7548421
Above is all of my code, but specifically this is the section I am working on, these have all been declared as var at top of script. I have updated this post with comments and suggestions so far from Basti. Code:
on('ready', function() {
_.each(monsterManual, function (monsterData){
log(monsterData.Name);
var character = createObj('character', {
name: monsterData.Name,
gmnotes: monsterData.FullText,
});
//*/
_.each(monsterAttributes, function(attr) {
var max = "";
var cur = "";
var re;
switch(attr){
case 'AC':
cur = parseInt(monsterData[attr].match(/(\d+)/)[1]);
break;
case 'Str':
case 'Dex':
case 'Con':
case 'Int':
case 'Wis':
case 'Cha':
re = new RegExp(attr + "\\s*(\\d*).*");
cur = parseInt(monsterData['AbilityScores'].match(re)[1]);
break;
case 'HD':
case 'Size':
case 'CR':
cur = monsterData[attr];
break;
case 'HP':
cur = parseInt(monsterData[attr]);
max = cur;
break;
case 'BaseAtk':
cur = parseInt(monsterData[attr]);
max = cur;
break;
case 'CMB':
cur = monsterData[attr];
max = cur;
break;
case 'CMD':
cur = parseInt(monsterData[attr]);
max = cur;
break;
case 'Acrobatics':
case 'Appraise':
case 'Bluff':
case 'Climb':
case 'Craft (any one)':
case 'Diplomacy':
case 'Disable Device':
case 'Disguise':
case 'Escape Artist':
case 'Fly':
case 'Handle Animals':
case 'Heal':
case 'Intimidate':
case 'Knowledge (religion)':
case 'Knowledge (planes)':
case 'Knowledge (history)':
case 'Knowledge (nature)':
case 'Knowledge (any one)':
case 'Linguistics':
case 'Perception':
case 'Ride':
case 'Sense Motive':
case 'Sleight of Hand':
case 'Spellcraft':
case 'Stealth':
case 'Survival':
case 'Swim':
case 'Use Magic Device':
re = RegExp(attr.replace('(', '\\(').replace(')', '\\)') + "\\s*(\\d*).*");
var match = re.exec(monsterData['Skills'])
if(match != null) {
cur = parseInt(monsterData['Skills'].match(re)[1]);
}
else {
cur = 0;
}
break;
default:
cur = parseInt(monsterData[attr]);
break;
}
log([attr, cur, max].join(':'))
if(cur != 0) {
createObj('attribute', {
characterid: character.id,
name: attr,
max: max,
current: cur
});
}
//*/
});
});
});
Basically
switch(attr) {
case 'AC':
//...
break;
case 'Str':
case 'Dex':
//...
break;
//...
}
will be
if(attr == 'AC') {
//...
} else if (attr == 'Str' || attr == 'Dex') {
//...
}
and so on. A case following another case directly will be converted to an or-expression.
BUT as the comments already point out, this is - with regard to readabilty - a bad idea...
You want to change this code section:
case 'Use Magic Device':
re = new RegExp(attr + "\\s*(\\d*).*");
cur = parseInt(monsterData['Skills'].match(re)[1]);
break;
to
case 'Use Magic Device':
re = RegExp(attr.replace('(', '\\(').replace(')', '\\)') + "\\s*(\\d*).*");
var match = re.exec(monsterData['Skills'])
if(match != null) {
cur = parseInt(match[1]);
} else {
cur = 0;
}
break;
This will check the match if it succeded (in this case the monster actually has the skill). If the match fails (the monst doesnt have this skill), then there won't be a parsing error and the break lets you hop out of the switch.
Further down you want to encase the call to createObject with an if:
if(cur != 0) {
createObj('attribute', {
characterid: character.id,
name: attr,
max: max,
current: cur
});
}
Furthermore, in your monsterAttributes there are two spelling errors which will cause cur to get NaN:
var monsterAttributes = [
/*...*/
'Disguise ', //there's a whitespace after Disguise, remove it!
/*...*/
'Use Magic device', //the 'd' has to be capital, as you check on 'Use Magic Device'
/*...*/
];
If I understood you correctly, you may simply check for attr being null and avoid the switch statement if this is the case. For example:
if( attr != undefined && attr != null ){
switch(attr){
//... the code as is in here
}// end switch
} // end if statement

Javascript case statement in the switch Statement

I have a problem with the 'case' statement in the 'switch' statement in java script.
My question is how to write more than one number in the 'case' statement and save all the work on writing multiple of commands for each number , ill try to explain myself better. i want to write in the case statement the
number 10-14 (10,11,12,13,14).
how can i write it?
thanks for helping and sorry for my bad english.
name = prompt("What's your name?")
switch (name)
{
case "Ori":
document.write("<h1>" + "Hello there Ori" + "<br>")
break;
case "Daniel":
document.write("<h1>" + "Hi, Daniel." + "<br>")
break;
case "Noa":
document.write("<h1>" + "Noa!" + "<br>")
break;
case "Tal":
document.write("<h1>" + "Hey, Tal!" + "<br>")
break;
default:
document.write("<h1>" + name + "<br>")
}
age = prompt ("What's your age?")
switch (age)
{
case "10":
document.write("you are too little" + name)
break;
case "14":
document.write("So , you are in junior high school" + name)
break;
case "18":
document.write("You are a grown man" + name)
break;
default:
document.write("That's cool" + name)
break;
}
Check out this answer Switch on ranges of integers in JavaScript
In summary you can do this
var x = this.dealer;
switch (true) {
case (x < 5):
alert("less than five");
break;
case (x > 4 && x < 9):
alert("between 5 and 8");
break;
case (x > 8 && x < 12):
alert("between 9 and 11");
break;
default:
alert("none");
break;
}
but that sort of defeats the purpose of a switch statement, because you could just chain if-else statments. Or you can do this:
switch(this.dealer) {
case 1:
case 2:
case 3:
case 4:
// Do something.
break;
case 5:
case 6:
case 7:
case 8:
// Do something.
break;
default:
break;
}
use this, if you dont provide break then control will fall down, In this way you can match for group of numbers in switch.
case 10:
case 11:
case 12:
case 14:
case 15: document.write("i am less than or equal to 15");break;
Say you wanted to switch on a number 10-14 (10,11,12,13,14) you can chain the cases together:
switch(number) {
case 10:
case 11:
case 12:
case 13:
case 14:
alert("I'm between 10 and 14");
break;
default:
alert("I'm not between 10 and 14");
break;
}
You can simply omit the break; statement.
switch (2) {
case 1: case 2: case 3:
console.log('1 or 2 or 3');
break;
default:
console.log('others');
break;
}
I wanted to play with the concept a bit, I do not recommend this approach, however you could also rely on a function that will create a control flow function for you. This simply allows some syntaxic sugar.
var createCaseFlow = (function () {
var rangeRx = /^(\d)-(\d)$/;
function buildCases(item) {
var match = item.match(rangeRx),
n1, n2, cases;
if (match) {
n1 = parseInt(match[1], 10);
n2 = parseInt(match[2], 10);
cases = [];
for (; n1 <= n2; n1++) {
cases.push("case '" + n1 + "':");
}
return cases.join('');
}
return "case '" + item + "':";
}
return function (cases, defaultFn) {
var fnStrings = ['switch (value.toString()) { '],
k;
for (k in cases) {
if (cases.hasOwnProperty(k)) {
fnStrings.push(k.split(',').map(buildCases).join('') + "return this[0]['" + k + "'](); break;");
}
}
defaultFn && fnStrings.push('default: return this[1](); break;');
return new Function('value', fnStrings.join('') + '}').bind(arguments);
};
})();
var executeFlow = createCaseFlow({
'2-9': function () {
return '2 to 9';
},
'10,21,24': function () {
return '10,21,24';
}
},
function () {
return 'default case';
}
);
console.log(executeFlow(5)); //2 to 9
console.log(executeFlow(10)); //10,21,24
console.log(executeFlow(13)); //default case
You have already gotten a few answers on how to make this work. However, I want to point out a few more things. First off, personally, I wouldn't use a switch/case statement for this as there are so many similar cases – a classic if/elseif/else chain feels more appropriate here.
Depending on the use-case you could also extract a function and then use your switch/case (with more meaningful names and values, of course):
function getCategory (number) {
if(number > 20) {
return 3;
}
if(number > 15) {
return 2;
}
if(number > 8) {
return 1;
}
return 0;
}
switch( getCategory( someNumber ) ) {
case 0:
// someNumber is less than or equal to 8
break;
case 1:
// someNumber is any of 9, 10, 11, 12, 13, 14, 15
break;
// ...
}
If the intervals are equally spaced, you could also do something like this:
switch( Math.floor( someNumber / 5 ) ) {
case 0:
// someNumber is any one of 0, 1, 2, 3, 4
break;
case 1:
// someNumber is any one of 5, 6, 7, 8, 9
break;
// ...
}
Also, it should be noted that some people consider switch/case statements with fall-throughs (= leaving out the break; statement for come cases) bad practice, though others feel it's perfectly fine.

Categories