Simple isEven function throws syntax error - javascript

I can't figure out why this function code will not run? Here is my code.
function isEven(n) {
if (n%2 == 0) {
return true;
};
else {
return false;
};
};
console.log(isEven(50));
I am getting an error message with the "else" statement.

Bit handy on the semi-colons
Should be
function isEven(n) {
if (n%2 == 0) {
return true;
}
else {
return false;
}
}
console.log(isEven(50));
Could even use
function isEven(n) {
return (n%2 == 0);
}
console.log(isEven(50));

Related

How to check if 2 strings are equal in JavaScript?

I'm a beginner in JS and trying to sort some cars by their model. The models are sorted by ranking in this order (Mercedes, BMW, Jeep, Nissan). I would like it to be case-insensitive. I went about it by creating a variable for creating the desired rankings.
var modelRanking = function(car) {
if (car.model.toLowerCase() === 'mercedes') {
return 1;
} else if (car.model.toLowerCase() === 'bmw') {
return 2;
} else if (car.model.toLowerCase() === 'jeep') {
return 3;
} else if (car.model.toLowerCase() === 'nissan') {
return 4;
} else {
return 5;
}
}
function modelComparator(car1, car2) {
if (car1.modelRanking < car2.modelRanking) {
return true;
} else if (car1.modelRanking > car2.modelRanking) {
return false;
} else if (car1.modelRanking == car2.modelRanking) {
return yearComparator(car1, car2);
}
}
However the modelRanking is always returning 5.
Instead of car1.modelRanking, use modelRanking(car1) because modelRanking is a function in global scope, not a property of car1.
function modelComparator(car1, car2) {
if (modelRanking(car1) < modelRanking(car2)) {
return true;
} else if (modelRanking(car1) > modelRanking(car2)) {
return false;
} else if (modelRanking(car1) == modelRanking(car2)) {
return yearComparator(car1, car2);
}
}

I am currently reading Eloquent JavaScript and I'm trying to solve the recursion test, but my code keeps giving me an 'undefined' feedback

This is the sample of my code:
Function isEven(number) {
(number == 1) {
return false;
}
else if (number == 0) {
return true;
}
else {
number += 2;
return isEven(-number);
}
};
This is the result I keep getting:
Console.log(isEven(50)); // undefined
Try using;
function isEven(number) {
if(number == 1) {
return false;
}
else {
if (number == 0) {
return true;
}
else {
number -= 2;
return isEven(number);
}
}
}
console.log(isEven(50));
You had a lot of syntax error as well as logical error. Note this logic only works for positive number. Hope it helps.

Are these JS conditional statements functionally equivalent?

Regarding conditional if/else statements, are the following examples functionally equivalent?
function isEntering() {
if (this.stage === 'entering') {
return true;
} else {
return false;
}
}
function isEntering() {
if (this.stage === 'entering') {
return true;
} return false;
}
function isEntering() {
if (this.stage === 'entering') {
return true;
}
}
isEntering = (this.stage === 'entering') ? true : false;
If so, I'd use the most terse of the options. But only if the four are functionally equivalent.
If expr is a boolean expression, as it is here, then there is no need to write
if (expr) return true;
else return false;
or to write
if (expr) x = true;
else x = false;
or to ever write
expr ? true : false
because being a boolean expression, expr can be returned, or assigned, directly:
return expr;
x = expr;
The tersest alternative is one you didn't give:
function isEntering() { return this.stage === 'entering'; }
They are not all equivalent.
The first two are equivalent, but:
function isEntering() {
if (this.stage === 'entering') {
return true;
}
}
Would return undefined if this.stage !== 'entering'.
Also:
isEntering = (this.stage === 'entering') ? true : false;
Is not defining a function as the other examples.
As mentioned you can add:
isEntering = () => this.stage === 'entering';
If you don't need a function you can use:
isEntering = this.stage === 'entering'

Return True or False Without the OR operator

I need to write a function called "or".
The instruction was:
Given 2 boolean expressions, "or" returns true or false, corresponding to the || operator.
Notes:
* Do not use the || operator.
* Use ! and && operators instead.
Here's my function:
function or(expression1, expression2) {
if(expression1 && !expression2) {
return true;
} else {
return false;
}
}
var output = or(true, false);
console.log(output); // --> IT MUST RETURN true;
Any idea what am I doing wrong?
try this:
function or(a, b) {
return !(!a && !b)
}
Update your code to following
function or(expression1, expression2) {
if(!expression1 && !expression2) {
return false;
} else {
return true;
}
}
function or(expression1, expression2) {
return !(!expression1 && !expression2);
}
console.log(or(true, false)); // --> IT MUST RETURN true;
console.log(or(true, true));
console.log(or(false, false));

I have similar code to this and im wondering if there is way to make it short

I am wondering how i can make them short in my js file. i have so many code similar to this. I have repeated this if function again and again also have to repeat else function as well.The only thing is changed is Mission() Doclick() and Yesdo(). If there is way to make it short let me know thanks.
function Buy() {
if (uida == '234' || uidb == '4563') {
Mission();
} else {
stop();
};
};
function Start() {
if (uida == '234' || uidb == '4563') {
Doclick();
} else {
stop();
};
};
function ReBuy() {
if (uida == '234' || uidb == '4563') {
Yesdo();
} else {
stop();
};
};
Use function pointers!
function Uida(fn) {
if (uida == '234' || uidb == '4563') {
fn();
} else {
stop();
};
}
function Buy() {
Uida(Mission);
};
function Start() {
Uida(Doclick);
};
function ReBuy() {
Uida(Yesdo);
}
Of course, you'll want to rename the Uida function to something more descriptive :)
You could look to make them all call one similar function and pass a uida paremeter, something like this:
// You could change the following 3 functions to have ternary statements
// eg: if ( uidaCheck() ) ? Function() : stop();
//
function Buy() {
if ( uidaCheck() ) {
Mission();
} else {
stop();
}
}
function Start() {
if ( uidaCheck() ) {
Doclick();
} else {
stop();
}
}
function ReBuy() {
if ( uidaCheck() ) {
Yesdo();
} else {
stop();
}
}
function uidaCheck() {
// uida assumed to be globally accessible var
//
if ( ( uida == '234' ) || ( uida == '4563' ) ) {
return true;
} else {
return false;
}
}
That's the theory anyway, gimme a sec and I'll see if I can tidy it up a bit. (ternary)
Here's a quick jsfiddle.

Categories

Resources