I'm working on a Codecademy exercise:
Remember the functions isOdd and isEven from Exercise 3.4?
We'd like you to code them here again! But this time, the aim is to define one function in terms of the other using the ! symbol.
Define isOdd, and then define isEven in terms of isOdd.
I tried a few different ways that I though would work,
like console.log(!isOdd(1));
and
(n !% 2 ===),
none of them are right.
Here is the code that I have:
var isOdd = function (n)
{
if (n % 2 ===0)
{
return false;
} else {
return true;
}
};
var isEven =function (n)
{
if (n % 2 === 0)
{
return true;
} else {
return false;
}
};
console.log(isOdd(1));
console.log(isOdd(2));
console.log(isOdd(999));
It's straightforward:
var isEven = function (n)
{
return !isOdd(n);
}
var isOdd = function(n){
if (n % 2 !== 0) {
return true;
} else {
return false;}
};
var isEven = function(n){
return !isOdd(n) ;
};
console.log(isOdd(11));
var isOdd = function(){
if(isEven){
return isEven();
} else {
return false;
}
};
var isEven = function (){
return true;
};
console.log(isOdd());
These answers are wrong by definition.
A number is an integer and float.
All submitted answers are good for integers.
Here are proper tailored responses.
function isEven(n) { // covers all
return (n).toString(2).endsWith(‘0’) // binary format
}
function isEven_integer(n) {
return (n & 1) === 0;
}
function isEven_float(n) {
// floats only, but works on integers.// faster for floats
return ((n).toString()[(n).toString().length-1] % 2) === 0; // base 10
}
var isOdd = function(n)
{
if(n%2 !== 0)
{ return ("odd");}
};
var isEven = function (n)
{
return !isOdd(n);
};
console.log(isEven(8));
Related
var yourself = {
fibonacci : function(n) {
return n === 0 ? 0 : n === 1 ? 1 :
this.fibonacci(n -1) + this.fibonacci (n-2)
}
};
This function is constantly setting the value of its 'fibonacci' property based on the
arguement supplied for 'n' parameter of the function.
I would like to refactor the function to reduce execution time
Using dynamic programming, Memoization that cache the already calculated result
read more about memoization here
const memoFib = function () {
let memo = {}
return function fib(n) {
if (n in memo) { return memo[n] }
else {
if (n <= 1) { memo[n] = n }
else { memo[n] = fib(n - 1) + fib(n - 2) }
return memo[n]
}
}
}
const fib = memoFib()
console.log(fib(50));
You could implement some kind of caching. This way you don't need to recalculate the same result multiple times.
var yourself = {
fibonacci : function(n, cache = new Map()) {
if(cache.has(n)) return cache.get(n);
if(n === 0) return 0;
if(n === 1) return 1;
const start = this.fibonacci(n-1, cache);
const end = this.fibonacci(n-2, cache);
cache.set(n-1, start);
cache.set(n-2, end);
return start + end;
}
};
console.log(yourself.fibonacci(40));
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
I know how modulus works in general, but it is not clear to me how the operator handles strings.
Recently, I had to write a script which checks if a name (string) contains an even number of letters. This actually worked, using modulus 2 and checking if result was 1 or 0:
function isNameEven(firstName) {
if (firstName % 2 === 0) {
return true;
}
else {
return false;
}
}
So I'm assuming the letters in the string were counted?
The result is always NaN
const oneLetter = "a";
const twoLetters = "ab";
const threeLetters = "abc";
console.log(oneLetter % 2);
console.log(twoLetters % 2);
console.log(threeLetters % 2);
Your function doesn't work if you pass it a string that can't be implicitly converted to a number that isn't NaN.
function isNameEven(firstName) {
if (firstName % 2 === 0) {
return true;
} else {
return false;
}
}
const oneLetter = "a";
const twoLetters = "ab";
const threeLetters = "abc";
console.log(isNameEven(oneLetter));
console.log(isNameEven(twoLetters));
console.log(isNameEven(threeLetters));
You could check the length property of the string though.
function isNameEven(firstName) {
if (firstName.length % 2 === 0) {
return true;
} else {
return false;
}
}
const oneLetter = "a";
const twoLetters = "ab";
const threeLetters = "abc";
console.log(isNameEven(oneLetter));
console.log(isNameEven(twoLetters));
console.log(isNameEven(threeLetters));
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)
Trying to come up with a function to check if a number is prime and I'm running into trouble. I'm sure there's a simpler way to do this, but why would this function not return false, for the number 9? It returns false for even numbers but for any other type of composite number it returns undefined, but since it prints NOT PRIME it should also be returning false.
function isPrime(n, i) {
document.writeln(i);
var nextNum = i + 1;
var number = n;
if (i < n) {
if ((n % i) === 0) {
document.writeln("NOT PRIME");
return false;
} else {
document.writeln(nextNum);
isPrime(number, nextNum);
}
} else if (i === n) {
document.writeln("Recursion ends");
return true;
} else {
document.writeln("Confused" + typeof i + typeof n);
}
}
You need to return the value of the recursive call, i.e., change
isPrime(number, nextNum);
to
return isPrime(number, nextNum);
You are missing a return in this branch after the recursive call to isPrime:
if ((n % i) === 0) {
document.writeln("NOT PRIME");
return false;
} else {
document.writeln(nextNum);
isPrime(number, nextNum);
}
I think that you want to change it to:
if ((n % i) === 0) {
document.writeln("NOT PRIME");
return false;
} else {
document.writeln(nextNum);
return isPrime(number, nextNum);
}
Because you aren't returning anything in that branch, the true/false calls are disappearing.
It should just need one parameter to check if prime.
Try this out:
function isPrime(num){
// An integer is prime if it is not divisible by any prime less than or equal to its square root
var squareRoot = parseInt(Math.sqrt(num));
var primeCountUp = function(divisor){
if(divisor > squareRoot) {
// got to a point where the divisor is greater than
// the square root, therefore it is prime
return true;
}
else if(num % divisor === 0) {
// found a result that divides evenly, NOT prime
return false;
}
else {
// keep counting
return primeCountUp(++divisor);
}
};
// start # 2 because everything is divisible by 1
return primeCountUp(2);
}
Adding the high of the "square root" from here