about detecting if a number is odd or even in javascript - javascript

I am working on a tutorial for JS on CodeAcademy and I am asked to create a function to check if the input is divisible by 2.
Here is my code:
var isEven = function(number) {
if (number % 2 ===0) {
return true;
}
else if (!isNaN(number)) {
return "Give a number";
}
else {
return false;
}
};
Why does codeacademy tell me that the code is wrong because the function will result in true for 3?

!isNaN(3) -> evaluates to true
if (typeof(number) === "number"){
return (number%2 === 0)
} else {
//do something else
return false
}

In JavaScript, anything that is not "falsy" is true. So, your string "Give a number" is considered to be true.

Reverse your sign on !isNaN i.e. remove the exclamation point - needs to be negative in construct
But really you shouldn't be returning text either.

Try this:
var isEven = function(number) {
if (!isNaN(number)) {
return "Give a number";
}
else if (number % 2 ===0) {
return true;
}
else {
return false;
}
};

Related

Function that takes a number as an argument and returns a boolean javascript

I am new to coding and I have this exercise where I have to write a function that takes a number as argument and returns a boolean. This is the code I wrote but is not working and I am getting the warning
"The function should only have a return statement in its body. You can evaluate a boolean expression an return immediately its value"
var even = function(x) {
if ((x % 2) === 0) {
return true;
} else
return false;
};
The response you get from the code submission has an important point:
The expression (x%2)===0 is already a boolean, so you can return that expression:
return x%2 === 0;
In general you should avoid this pattern:
if (some_boolean_expression) {
return true;
} else {
return false;
}
... since the boolean you return is exactly the same as the boolean expression that is evaluated in the if condition. So it should be just:
return some_boolean_expression;
you can just write your function like this
var even = function(x)
{
return x % 2 === 0
}
var even = function(x) {
if (typeof x === "number") {
if (x % 2 === 0) {
return true
} else {
return false
}
} else {
return false
}
}
This covers all the edge cases

Javascript program yields undefined (recursion) [duplicate]

This question already has answers here:
Simple Recursive Javascript Function Returns Undefined
(2 answers)
Closed 4 years ago.
Hello I am a newbie learning Js
I am trying to learn about recursion but I stuck in here
var isEven = (number) =>{
number = Number(number)
if(number === 0){
console.log('it is even')
return true;
}
else if(number === 1){
return false;
}
else{
number = number - 2;
isEven(number);
}
}
console.log(isEven(50) === true)
why the end result becomes undefined? Thank you for the help
Add return in recursion call:
function isEven(number){
number = Number(number)
if(number === 0){
console.log('it is even');
return true;
}
else if(number === 1){
return false;
}
else{
number = number - 2;
return isEven(number);
}
}
console.log(isEven(50));
You must use return in recursion call .If u do not use return the isEven(50) function will run but do not return isEven(48) so your function isEven(50) get undefined.So always use return.
Example
function factorial( n ) {
if ( n === 1 ) {
return 1;
}
return n * factorial( n - 1 );
}
In above example you can when we call factorial(n-1); it will return (n-1)*factorial(n-2); but if u remove the return then result is undefined as factorial(n-1); do not return anything.
Always remember in recursion focus is on returning function again and again till we get the result.

isNan help in an if else statement

I am doing an online exercise and I am required to use an if else statement. The isNaN statement is not working. I am required to return a string if the input of number is not an actual number. This won't compile please help:
var isEven = function(number) {
if (number % 2 === 0) {
return true;
} else if (number % 2 !== 0) {
return false;
} else if (isNaN(number)) {
return "you need to enter a number";
} else {
return false;
}
};
Your code returns false because of
else if (number % 2 !== 0)
line, So check isNaN before everything like so
var isEven = function(number) {
if (isNaN(number)) {
return "you need to enter a number";
} else {
if (number % 2 === 0){
return true;
} else if (number % 2 !== 0) {
return false;
}
}
};
console.log(isEven(NaN));
you need to enter a number
You need to have isNan as first check in you if-else-if condition.
Explanation:
If the argument is not a number, then number % 2 !== 0 will be true and it will return the value.
Note:
return statement, terminates the execution of the function further and returns the value. So, even if-else-if is not required here.
and beware with Booleans in NaN and empty string.
Example Snippet:
var isEven = function(number) {
if (isNaN(number)) {
return "you need to enter a number";
}
if (number % 2 === 0) {
return true;
} else {
return false;
}
};
console.log(isEven(2));
console.log(isEven(3));
console.log(isEven('i am not a number'));
console.log(isEven(true));

Prime factor in javascript, why is this case not working?

I'm writing a primality checker in in j/s and I was wondering why I'm getting true as a return for when I test 55... It seems to work fine for all the other cases I checked just not 55, can someone tell me where I went wrong?
var isPrime = function(num){
if (num === 2){
return true;
}
else if(num%2 === 0){
return false;
}
else{
var i = 2;
while(i<num){
if((num/i) % i === 0 ){
return false;
}
i++
}
return true;
}
};
Thanks in advance and apologies for the noobness!
if((num/i) % i === 0 ){
return false;
}
What is this case?
Shouldn't it be
if(num % i === 0 ){
return false;
}
Just as #Andrey pointed out, your if statement inside the while loop is not correct. For 55 at i=5 you should get false for 55 being prime, but 55/5 % 5 == 1 Also you could use just == instead of === for logical equals, since === checks if both values and type are equal, which is not necessary here.
Try this.
var isPrime = function isPrime(value) {
var i = 2;
for(i; i < value; i++) {
if(value % i === 0) {
return false;
}
}
return value > 1;
};
Even if your bug may be solved, I'd recommend to think about some other aspects to optimize your code:
Clarify your corner cases: In the beginning, check for n<2 -> return false. At least to math theory primes are defined as natural number larger then 1, so 1 is by definition not a prime like all the negative numbers. Your code won't handle negative numbers correctly.
You don't have to check all divisors up to n-1. You can obviously stop checking at n/2, but there are even proofs for tighter bounds which means you can stop checking already at √n if I'm right. To further optimize, you don't have to check even divisors >2.
else {
var i = 3;
while ( i < num/2 ) {
if( num % i == 0 ) {
return false;
}
i+=2;
}
return true;
}
See https://en.wikipedia.org/wiki/Primality_test for details about testing of primes.
P.S: I just wrote the code here in the text box, but it looks like it might work.

How do I check if a JavaScript parameter is a number?

I'm doing some trouble-shooting and want to add a check that a parameter to a function is a number. How do I do this?
Something like this...
function fn(id) {
return // true iff id is a number else false
}
Even better is if I can check that the parameter is a number AND a valid integer.
function fn(id) {
return typeof(id) === 'number';
}
To also check if it’s an integer:
function fn(id) {
return typeof(id) === 'number' &&
isFinite(id) &&
Math.round(id) === id;
}
i'd say
n === parseInt(n)
is enough. note three '===' - it checks both type and value
Check if the type is number, and whether it is an int using parseInt:
if (typeof id == "number" && id == parseInt(id))
=== means strictly equals to and == checks if values are equal.
that means "2"==2 is true but "2"===2 is false.
using regular expression
var intRegex = /^\d+$/;
if(intRegex.test(num1)) {
//num1 is a valid integer
}
example of
== vs. ===
function fn(id){
if((parseFloat(id) == parseInt(id)) && !isNaN(id)){
return true;
} else {
return false;
}
}
function fn(id) {
var x = /^(\+|-)?\d+$/;
if (x.test(id)) {
//integer
return true;
}
else {
//not an integer
return false;
}
}
Test fiddle: http://jsfiddle.net/xLYW7/

Categories