isNan help in an if else statement - javascript

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));

Related

Check if first character of a string is a letter in JavaScript

I'm trying to write a function that checks a string for multiple conditions. However, I have reached a wall when trying to figure out how to check if the first character in a string is a letter only.
function SearchingChallenge(str) {
// code goes here
let onlyLetters = /^[a-zA-Z]+$/;
if (str.length > 4 && str.length < 25){
if (onlyLetters.test(str)){
return true;
} else {
return false;
}
} else {
return false;
}
}
"u__adced_123" should return true but it's returning false. I've tried str[0]==onlyLetters but still the same.
onlyLetters.test(str) checks the whole string. To get the first character, use str.charAt(0).
function SearchingChallenge(str) {
let onlyLetters = /^[a-zA-Z]+$/;
if (str.length > 4 && str.length < 25) {
if (onlyLetters.test(str.charAt(0))) {
return true;
} else {
return false;
}
} else {
return false;
}
}
console.log(SearchingChallenge('Hello World!'));
console.log(SearchingChallenge('!dlroW olleH'));
console.log(SearchingChallenge('u__adced_123'));
const SearchingChallenge = str => (
!!str[4] && // > 4
!str[24] && // < 25
(/^[a-z]+$/i).test(str) // alpha
);
// Tests...
// true: greater than four characters
console.log(SearchingChallenge('Fiver'));
// true: less than twenty-five characters
console.log(SearchingChallenge('TwentyFouroooooooooooooo'));
// false: twenty-five or more characters
console.log(SearchingChallenge('TwentyFiveooooooooooooooo'));
// false: contains numbers
console.log(SearchingChallenge('abcd1234'));
// false: less than five characters
console.log(SearchingChallenge('Four'));

When ever i call this function, it does not tell me its a prime number when i know it is a prime number e.g. 5

Prime number problem on JavaScript. Whenever I ask the user to enter an input and then run the code below, it returns false always even if I know the number is a prime.
let number = prompt("Enter a number to test as a prime number:");
let divisor = 2;
function isPrime(number, divisor) {
if (number == 1) {
return false;
}
else if (number == 2) {
return true;
}
else if (number % divisor == 0) {
return false;
}
else if (number % divisor !== 0) {
return true;
}
else if (divisor * divisor > number) {
return true;
}
else if (divisor > number) {
return true;
}
else {
divisor++;
return false;
}
}
alert(isPrime(number, divisor));
console.log(number);

Why does function return undefined when it explicitly is told to return true?

I was trying to solve the following coding exercise.
We have two special characters. The first character can be represented
by one bit 0. The second character can be represented by two bits (10
or 11).
Now given a string represented by several bits. Return whether the
last character must be a one-bit character or not. The given string
will always end with a zero.
example:
Input: bits = [1, 0, 0] Output: True
Below is my solution for the above challenge. Why is this returning undefined? If I use [1,0,1,0] as input, it should return true but I am getting undefined. I am explicitly writing true in the return statement and not the results of a variable.
var isOneBitCharacter = function(bits) {
console.log(bits);
var length = bits.length;
if (length == 1) {
return true;
}
if (length == 0) {return false;}
if (length == 2 && bits[0] === 1) {
return false;
}
if (bits[0] === 1) {
isOneBitCharacter(bits.slice(1));
} else {
isOneBitCharacter(bits.slice(2));
}
};
isOneBitCharacter([1,0,1,0]);
I guess you are missing returns. Here is adjusted code:
var isOneBitCharacter = function(bits) {
console.log(bits);
var length = bits.length;
if (length == 1) {
return true;
}
if (length == 0) {return false;}
// added return here and next statements
if (length == 2 && bits[0] === 1) {
return false;
}
if (bits[0] === 1) {
return isOneBitCharacter(bits.slice(1));
} else {
return isOneBitCharacter(bits.slice(2));
}
};
isOneBitCharacter([1,0,1,0]);

How to check if number is even using recursive function in JavaScript

I am trying to do an exercise from here https://www.w3resource.com/javascript-exercises/javascript-recursion-function-exercise-7.php
I understand the solution, however, I am kind of curious why my program does not yield the same answer as above. The way I choose the return the function is slightly different, however, it yields a number instead of true or false.
function checkeven(num) {
if (num === 0) {
return true;
} else if (num === 1) {
return false;
} else {
console.log(num);
return num - checkEven(num - 2);
}
}
console.log(checkeven(8));
console.log(checkeven(9));
In JavaScript - when the operands to an operator are of different types - type corecion happens, i.e it tries to convert one of the operands into a type that is compatible with the operator
Try this:
console.log(2 + true) //3
console.log(true + false) //1
In your case, consider what happens when you try checkeven(2)
checkeven(2) = 2 - checkeven(0) = 2 - true = 1
Replace return num - checkeven(num - 2) with return checkeven(num - 2) in your code.
function checkeven(num) {
if (num === 0) {
return true;
} else if (num === 1) {
return false;
} else {
return checkeven(num - 2);
}
}
console.log(checkeven(8));
console.log(checkeven(9));
Concocted a solution with both positive and negative numbers!
var isEven = function (n) {
if (n > 0) {
return positive(n);
} else {
return negative(n);
}
function negative(num) {
if (num === 0) {
return true;
} else if (num === -1) {
return false;
} else {
return isEven(num + 2);
}
}
function positive(num) {
if (num === 0) {
return true;
} else if (num === 1) {
return false;
} else {
return isEven(num - 2);
}
}
};
console.log(isEven(9));
console.log(isEven(8));
console.log(isEven(-8));
your code had issue since you we trying to subtract from a boolean value , Due to which type corecion occurs as per the rules given under ecmascript guidlines.(https://www.ecma-international.org/ecma-262/7.0/#sec-ordinary-and-exotic-objects-behaviours)
you can go with the following solution:
(function fixthis() {
function checkeven(num) {
if (num === 0) {
return true;
} else if (num === 1) {
return false;
} else {
let Tempnum = num - 2;
if (checkeven(Tempnum)) {
return true;
} else {
return false;
}
}
}
console.log(checkeven(8));
console.log(checkeven(9));
})()
return checkeven(num-2);, returns a boolean value. You are trying to subtract a boolean value from a number, which won't work. Your code will work just fine if you remove num-checkeven(num-2)* and replace it with checkeven(num-2);
You can do:
odd = function(n) {if (n%2 == 0) {return true} else {return false;}}
console.log(odd(8));
console.log(odd(5));
or:
checkeven = function(num) {
if (num === 0) {
return true;
} else if (num === 1) {
return false;
} else {
return checkeven(num - 2);
}
}
console.log(checkeven(8));
console.log(checkeven(9));
You can also use the modulo operator, so you don't even need recursion
const areYouEven = n => !(n % 2)

about detecting if a number is odd or even in 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;
}
};

Categories