I'm writing code in vanilla JavaScript but I don't want to write a thousand different if statements.
I already tried searching up how to reduce if statements in JavaScript, but I didn't find anything helpful.
Here is some example code:
if (a == "text" && b == "othertext") {
console.log("message");
} else if (a == "text2" && b == "othertext2") {
console.log("other message");
} else if (a == "text3" && b == "othertext3") {
console.log("other other message");
} else if (a == "text4" && b == "othertext4") {
console.log("other other other message");
} else if (a == "text5" && b == "othertext5") {
console.log("other other other other message");
} else if (a == "text6" && b == "othertext6") {
// .. and so on.
}
If anyone can help me, it would be appreciated
You can use a data-driven approach by using the strings as keys in an object.
const messages = {
"text|othertext": "message",
"text1|othertext1": "message1",
"text2|othertext2": "message2"
};
function showMessage(a, b) {
let key = `${a}|${b}`;
if (messages.hasOwnProperty(key)) {
console.log(messages[key]);
} else {
console.log("Invalid a and b");
}
}
showMessage("text", "othertext");
You could use ternary operators I suppose.
let msg = '';
msg = a === 'text' && b === 'othertext' : msg;
msg = a === 'text2' && b === 'othertext2' : msg;
// etc.
Ultimately its not gonna get much prettier but that might be a little bit simpler to type.
Related
I try to get rid of an ugly javscript eval method (Cause we all know it is unsecure).
I have the following problem. I build a dynamic searchstring.
Depends on the TLD a user decided to search for.
Here is my code:
if (tld == 0) {
var searchString = 'value.tld != ""';
}
if (tld == 1) {
var searchString = 'value.tld == "de"';
}
if (tld == 2) {
var searchString = 'value.tld == "com" || value.tld == "net" || value.tld == "org" || value.tld == "info" || value.tld == "biz"';
}
if (tld == 3) {
var searchString = 'value.tld == "io"';
}
Depending on the search parameter 'searchstring', I build this routine with eval:
if (eval(searchString)) {
// Do something special, depends on the tld variable
}
How can i rebuild this without using 'eval'. The premission is, that the first part of the code is beeing untouched.
Thanks in advance
Nick
How about:
let choices = {
1: ['de'],
2: ['com', 'net', 'org', 'info', 'biz'],
3: ['io']
};
function check(tldparam) {
if (tld === 0) {
return value.tld !== "";
} else {
return tld === tldparam && choices[tldparam].includes(value.tld);
}
}
And we test it like:
// Got this value from somewhere
let tld = 2;
let value = {tld: 'net'};
// This is my checking criterion
let tldparam = 2;
if (check(tldparam)) {
// Do something special, depends on the tld variable
}
Does it serve your purpose?
I have a concise if/else statement below:
function () {
if (elem.attr('data-src-1') === '' && elem.attr('data-src-2') === '') {
// scenario a
} else if (elem.attr('data-src-1') === '' && elem.attr('data-src-2') !== '') {
// scenario b
} else if (elem.attr('data-src-1') !== '' && elem.attr('data-src-2') === '') {
// scenario c
} else {
// scenario d
}
}
which is returning a complexity of 7 by strict linting rules. I need to reduce its complexity to 6 but can't see how to make it more concise?
More readable one (at least for me)
let data1 = elem.attr('data-src-1') === ''
let data2 = elem.attr('data-src-2') === ''
if (data1)
!data2 ? console.log(" scenario a ") : console.log(" scenario b ")
else
data2 ? console.log(" scenario c ") : console.log(" scenario d ")
This is more of a code review question, but you could combine the if 1='' into if/elses, then do the same for the interiors if/elses.
I think this is less readable, but it is technically less complex.
function() {
if (elem.attr('data-src-1') === '') {
if (elem.attr('data-src-2') === '') {
// scenario a
}
else {
// scenario b
}
} else if (elem.attr('data-src-2') === '') {
// scenario c
}
else {
// scenario d
}
}
I have strings that I have to insert in a db but I want to first modify their value if they fall under certain conditions.
For example I have the strings Epatite, Epatite B, EpatiteáB, EpB3 that I want them to be changed to EP B before being inserted into the db.
This is piece of my code:
// vaccines[index] is the string to compare
var vac = makeUniform(vaccines[index]);
const queryInsert = {
text: 'INSERT INTO coverages (vaccine) VALUES ($1) ON CONFLICT DO NOTHING;',
values: [vac]
}
var printText = '[INSERT Italy IN coverages]';
promises.push(postgreSQLlib.query(queryInsert, printText, false));
function makeUniform(val) {
if(val === 'DIF' || val === 'Difterite') {
return 'DIPH'; // diphtheria
}
else if(val === 'Epatite' || val === 'Epatite B' || val === 'EpatiteáB' || val === 'EpB3') {
return 'EP B'; // hepatitis B
}
else if(val === 'HIB' || val === 'Hib3' || val === 'Hib') {
return 'HIB'; // haemophilus influenzae B
}
else {
return val;
}
}
Whene I execute SELECT DISTINCT vaccine FROM coverages ORDER BY vaccine; on psql shell, I get:
DIPH
DT-DTP3
DTP3
EP A
EP B
EpatiteáB
Hib
HIB
M-MPR1
M-MPR1-MPRV ...
There is EpatiteáB which theoretically should have changed in EP B.
Why it doesn't work?
EDIT 1
vaccines[index] comes from an online pdf of which I did web scraping using the textract package of Node.js.
Thanks
Try to clean your development database first with this:
UPDATE coverages set vaccine = 'EP B' WHERE vaccine LIKE 'Epatite%' OR vaccine = 'EpB3';
Do something similar for the others.
Try this added one more condition = (val==="Epatite%E1B%21")
function makeUniform(val) {
if(val === 'DIF' || val === 'Difterite') {
return 'DIPH'; // diphtheria
}
else if(val === 'Epatite' || val === 'Epatite B' || val==="Epatite%E1B%21" || val === 'EpatiteáB' || val === 'EpB3') {
return 'EP B'; // hepatitis B
}
else if(val === 'HIB' || val === 'Hib3' || val === 'Hib') {
return 'HIB'; // haemophilus influenzae B
}
else {
return val;
}
}
if ((value.length == 12) || (value.length == 9)) {
if ((value.length == 12)) {
if (value.substring(0, 2) = "048") { //this doesn't work in the execution
return true;
} else {
return false;
}
}
if ((value.length == 9)) {
return true;
} else {
return false;
}
} else {
return false;
}
You need == like this. you cant have a single = in an if statement
if (value.substring(0,2)=="048"){
It is because you are using the JS assignment operator. Typically var a = 123;
You want to be using === since it doesn't do type coercion. As opposed to == which does.
if (value.substring(0,2) === "048") {
// etc
}
Is it possible to do something like this in JavaScript?
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
for (i in exampleObject) {
else if (name == exampleObject[i].name) {
exampleFunction(exampleObject[i].arg);
}
}
else {
exampleFunction('default')
}
I tried it, but got an "unexpected keyword else on line 8" (the "else if" within the for loop). Is there another way to do this?
edit: updated this to use exampleObject[i] in the loop. My bad!
No. I think the best way to accomplish this is to move the for loop into an else block and do the following
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
var isFound = false;
for (i in exampleObject) {
if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
isFound = true;
}
}
if (!isFound) {
exampleFunction('default')
}
}
Note: It looks like there are other errors in this code. The for loop declares the i iteration variable but never actually uses it. Did you mean for the if check in the for loop to use i instead of name?
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
var isFound = false;
for (i in exampleObject) {
if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
isFound = true;
break;
}
}
if (!isFound) {
exampleFunction('default')
}
}
Here is the correct solution. It short circuts the if statements in the loop just like else if would short circuit. This is the same solution as #1 but it correctly short circuits.
The following code looks wrong to me , have the for loop inside if block
for (i in exampleObject) {
else if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
}
that is not possible. I would try an come up with a better example to show you how to do what you want, but honestly I am not sure what you want to do. The for loop is confusing me. Can you provide some more information?
In a word, no. You are terminating the if-statement block with the last brace before the for statement.
Well for one, shouldn't this:
for (i in exampleObject) {
else if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
}
}
be this:
for (i in exampleObject) {
else if (name == i.name) {
exampleFunction(i.arg);
}
}
Though i don't know much (if anything) about js, this is just a guess at something that isn't even the problem you're talking about.
Would you be adverse to doing it like this:
bit = 0;
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
bit = 1;
}
bit2 = 0;
while(bit == 1){
for (i in exampleObject) {
if (name == i.name) {
exampleFunction(i.arg);
bit = 0
bit2 = 1;
}
}
}
if(bit2 = 0){
exampleFunction('default');
}
?
Something like this may help?
found = false
if (name == 'foo') {
found = true
exampleFunction('some_arg');
}
else if (name == 'bar') {
found = true
exampleFunction('another_arg');
}
else {
for (i in exampleObject) {
if (name == i.name) {
exampleFunction(i.arg);
found = true
break;
}
}
}
if !found:
exampleFunction('default')