Instead of critique from a person, Iāll use JSLint to supply the critique instead. Itās much easier to accept critique from an inhuman machine, than from someone else.
Expected ā:ā and instead saw ā=ā.
Assignments within ternary statements are bad, as they easily lead to confusion.
A better solution is to assign the ternary to result.
Iāll comment out the old code to make it easier to see how we get from the old code to the new code.
// var result;
// a>b ? result = ["firstNumber", a] : result = ["secondNumber", b];
var result = a>b ? ["firstNumber", a] : ["secondNumber", b];
Wrap a ternary expression in parens, with a line break after the left paren.
Thatās kind of specific. letās see what we get when we do that.
// var result = a>b ? ["firstNumber", a] : ["secondNumber", b];
var result = (
a>b ? ["firstNumber", a] : ["secondNumber", b]
);
Undeclared āfirstNumberā.
That should be declared properly using var or const. As youāre using var in your code Iāll remain consistent with that.
// firstNumber = 125
var firstNumber = 125
Expected ā;ā and instead saw āsecondNumberā.
Statements should be consistently ended with a semicolon.
// var firstNumber = 125
var firstNumber = 125;
Undeclared āsecondNumberā.
Weāll do the same as above.
// secondNumber = 17
var secondNumber = 17;
Undeclared āconsoleā.
Thatās fine, console is short for window.console, but instead of changing console to window.console Iāll make a rare exception in regard to console and add a declaration at the top that console is allowed as a global variable.
/*jslint browser */
/*global console */
...
Line is longer than 80 characters.
// console.log(numberResult[1] + " is larger. Therefore, the bigger number is " + numberResult[0]);
console.log(
numberResult[1] + " is larger. " +
"Therefore, the bigger number is " + numberResult[0]
);
Expected one space between ā,ā and ābā.
Now that weāre on to formatting issues, most of them are easily resolved by passing the code through jsBeautifer.
/*jslint browser */
/*global console */
function theNumber(a, b) {
var result = (
a > b ? ["firstNumber", a] : ["secondNumber", b]
);
return result;
}
var firstNumber = 125;
var secondNumber = 17;
var numberResult = theNumber(firstNumber, secondNumber);
console.log(firstNumber);
console.log(secondNumber);
console.log(
numberResult[1] + " is larger. " +
"Therefore, the bigger number is " + numberResult[0]
);
Expected ā?ā at column 8, not column 14.
Ternary statements should have their different parts made more easily visible.
// var result = (
// a > b ? ["firstNumber", a] : ["secondNumber", b]
// );
var result = (
a > b
? ["firstNumber", a]
: ["secondNumber", b]
);
And JSLint is finally satisfied.
No, the function is not comparing anything about the arrays.
Instead, itās comparing a and b, and depending on which one is larger, the function is returning one array or another.
The code will be easier to understand when it is as an if statement instead.
// var result = (
// a > b
// ? ["firstNumber", a]
// : ["secondNumber", b]
// );
// return result;
var result;
if (a > b) {
result = ["firstNumber", a];
} else {
result = ["secondNumber", b];
}
And now itās easier to see that the result value is being returned from the function, no matter the result of the if statement. We can do without the result variable completely and just return the array.
// var result;
// if (a > b) {
// result = ["firstNumber", a];
// } else {
// result = ["secondNumber", b];
// }
if (a > b) {
return ["firstNumber", a];
} else {
return ["secondNumber", b];
}
Even though anonymous functions are possible, you should always name your functions so that you and/or other people reading the code have an easier time understanding what is going on.