ltpitt
asked on
Optimize simple Javascript code to use no repetitions
I have this working code:
I would really like to remove repeated code, how can I do it?
function setvar(name, value) {
if (this.hasOwnProperty(name)) { //typos happen
this[name] = value;
myOtherFunction.setValue(value);
} else {
}
}
if (Variable1 == "TBD") {
setvar("StdErr",StdErr.concat("\nValidation for Variable1 failed, no valid value found\n");
setvar("ComponentState","Failed");
}
if (Variable2 == "TBD") {
setvar("StdErr",StdErr.concat("\nValidation for Variable2 failed, no valid value found\n");
setvar("ComponentState","Failed");
}
if (Variable3 == "TBD") {
setvar("StdErr",StdErr.concat("\nValidation for Variable3 failed, no valid value found\n");
setvar("ComponentState","Failed");
}
if (Variable4 == "TBD") {
setvar("StdErr",StdErr.concat("\nValidation for Variable4 failed, no valid value found\n");
setvar("ComponentState","Failed");
}
I would really like to remove repeated code, how can I do it?
ASKER
That part can be simply ignored.
Consider it as a function that needs to becalled all of the times to update a variable.
It is not changing the code you see because it is just a function receiving data: it is not returning anything.
Consider it as a function that needs to becalled all of the times to update a variable.
It is not changing the code you see because it is just a function receiving data: it is not returning anything.
Try this ;)
Create an array of variables you want to check:
By this way you can check as many variables you want to check;
Create an array of variables you want to check:
variables = [Variable1, Variable2, Variable3, Variable4];
for(var i = 0, len = variables.length; i < len; i++){
if(variables[i] == 'TBD'){
setvar("StdErr", StdErr.concat("\nValidation for Variable" + (i + 1) + " failed, no valid value found\n");
setvar("ComponentState", "Failed");
}
}
By this way you can check as many variables you want to check;
try:
function setvar(name, value) {
if (this.hasOwnProperty(name)) { //typos happen
this[name] = value;
myOtherFunction.setValue(value);
} else {
}
}
var msg = '';
if (Variable1 == "TBD") {
msg = "\nValidation for Variable1 failed, no valid value found\n";
}
if (Variable2 == "TBD") {
msg = "\nValidation for Variable2 failed, no valid value found\n";
}
if (Variable3 == "TBD") {
msg = "\nValidation for Variable3 failed, no valid value found\n";
}
if (Variable4 == "TBD") {
msg = "\nValidation for Variable4 failed, no valid value found\n";
}
if( msg != '' ) {
setvar("StdErr",StdErr.concat( msg );
setvar("ComponentState","Failed");
}
ASKER
I've written a more efficient solution:
// Here I create an hashmap containing all the values
var obj = {};
obj.Conn_IP = Conn_IP;
obj.Conn_IPmon = Conn_IPmon;
obj.Conn_Pass = Conn_Pass;
obj.Conn_User = Conn_User;
// I use a loop to remove code redundancy and use both key and value from the hashmap
for(key in obj)
if (obj[key] == 'TBD') {
StdErr += "\nValidation for " + key + " failed, no valid value found\n"
ComponentState = "Failed";
}
Try this ;)
Removed repetition:
Removed repetition:
var obj = {
Conn_IP,
Conn_IPmon,
Conn_Pass,
Conn_User
};
// I use a loop to remove code redundancy and use both key and value from the hashmap
for(key in obj)
if (obj[key] == 'TBD') {
StdErr += "\nValidation for " + key + " failed, no valid value found\n"
ComponentState = "Failed";
}
}
ASKER
Hi @Mukesh,
I tried your code but it seems like it is not working (changed my code slightly to implement it):
I tried your code but it seems like it is not working (changed my code slightly to implement it):
// Component variables
Conn_IP = 'TBD';
Conn_IPmon = 'TBD';
Conn_Pass = 'TBD';
Conn_User = 'TBD';
// Error variables
ComponentState = "Working";
StdErr = "";
// Here I create an hashmap containing all the values
var obj = {
Conn_IP,
Conn_IPmon,
Conn_Pass,
Conn_User
};
// I use a loop to remove code redundancy and use both key and value from the hashmap
for(key in obj)
if (obj[key] == 'TBD') {
StdErr += "\nValidation for " + key + " failed, no valid value found\n"
ComponentState = "Failed";
}
}
// Printing error variables to console for testing
console.log("Final ComponentState: " + ComponentState);
console.log("Final StdErr: " + StdErr);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When you are creating object properties same as a variable name then you can only pass variable name, it will add a property and will assign that variable value to property. Like:
var obj = {
Conn_IP,
Conn_IPmon,
Conn_Pass,
Conn_User
};
ASKER
Can you please post your full tested code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now I understand!
It is a beauty, very well done :)
It is a beauty, very well done :)
Open in new window