Reputation: 2172
In a preliminary technical interview, I was asked to write a simple calculator function in Javascript. My code was passable but he commented on my bad spacing. I wrote something like this:
var calc = function(num1, num2, operand){ //function(... VS function (...
if(operand === 'add'){
return num1 + num2;
} else if(operand === 'multiply'){ // if(...
return num1 * num2;
} else if (operand === 'subtract'){ // if (...
return num1 - num2;
} else {
console.log("Not a valid operand");
};
};
I am a beginner in Javascript looking to learn and maintain good coding habits. I understand the function above would run regardless of my inconsistent spacing, but is there a correct way of spacing Javascript control loops?
Any advice or coding examples will help! Thanks!
Upvotes: 4
Views: 7420
Reputation: 7466
He probably thought it was bad because you write in a different style than he does.
A good resource on code style is the Google JavaScript Style Guide.
The key point at the end of the page says:
BE CONSISTENT.
If you're editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around all their arithmetic operators, you should too. If their comments have little boxes of hash marks around them, make your comments have little boxes of hash marks around them too.
The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you're saying rather than on how you're saying it. We present global style rules here so people know the vocabulary, but local style is also important. If code you add to a file looks drastically different from the existing code around it, it throws readers out of their rhythm when they go to read it. Avoid this.
Upvotes: 8
Reputation: 8379
Does this work for you?
var calc = function(num1, num2, operand){
return ( new Function( 'return ' + num1 + operand + num2 ) )();
};
And as others already mentioned, there are no white space rules like Python or F# in JavaScript. He might said bad spacing due to below reasons
If you use if() else()
, for every comparison value has to retrieved from memory, if switch is used, the value will be retrieved once and do the job. Ofcourse Switch is better in this case.
Using switch is also not necessary in this case. You can use as I have mentioned in answer.
Upvotes: 0
Reputation: 5515
It's all really just a matter of opinion. Your code is perfectly acceptable - slightly inconsistent in places, but valid nonetheless. I personally would write it something like this:
var calc = function (num1, num2, operand) {
if (operand === 'add') {
return num1 + num2;
} else if (operand === 'multiply') {
return num1 * num2;
} else if (operand === 'subtract') {
return num1 - num2;
} else {
console.log("Not a valid operand");
}
};
Well, I'd actually probably use a switch statement but anyway...
Here are a few style guides you might find useful:
Of course, none of these are the right answer, but they can all help keep your code clean and maintainable.
Upvotes: 3