I am a beginner in Javascript now I have started, the only background I have is HTML and CSS. I'm trying to make a program that prints whether a number is even or odd. But to the odd numbers to add 2 and 4. My code :
function isEvenExceptTwoOrFour(number) {
if (number%2 == 0 ) {
console.log("The number is even");}
else {
console.log("The number is odd ")
}
}
You could write an if..else statement like this, using Logical Or (||) to check each of your conditions.
Below I used the statement
if (number === 2 || number === 4 || number % 2 === 1)
This checks if number === 2 or number === 4 or number % 2 === 1 (if the number is odd)
Code:
function isEvenExceptTwoOrFour(number) {
if (number === 2 || number === 4 || number % 2 === 1) {
console.log("Number is considered odd");
} else {
console.log("Number is considered even")
}
}
isEvenExceptTwoOrFour(1);
isEvenExceptTwoOrFour(2);
isEvenExceptTwoOrFour(6);
Write a function that accepts an array of exceptions, and returns a new function that accepts a number. The closure (the function that's returned) will then 1) check to see if the number is in the array, and return false otherwise 2) check to see if the number is even, and return true, otherwise 3) return false.
// Pass in the exceptions array and return a function
// that will accept a number
function checkIsEvenExcept(exceptions) {
return function (n) {
if (exceptions.includes(n)) return false;
return n % 2 === 0 && true;
return false;
}
}
const exceptions = [2, 4, 18];
// Assign the result of calling `checkIsEvenExcept` with the
// exceptions array to a variable. This will be the function that
// we can call
const isEven = checkIsEvenExcept(exceptions);
// We can now call that function with a number
// that we need to check
console.log(isEven(6));
console.log(isEven(2));
console.log(isEven(1));
console.log(isEven(4));
console.log(isEven(8));
console.log(isEven(18));
You can just add conditions whether the number is 2 or 4.
function isEvenExceptTwoOrFour(number) {
if ( number === 2 || number === 4 ||| number % 2 !== 0){
console.log("The number is odd ")
return
}
console.log("The number is even")
}
Related
I am doing an exercise, the problem is that my if/else structure does not work properly and I do not know why.
Here is the exercise statement and my code
Your task is to write a function, fizzBuzz, that accepts a number and returns a string:
'fizz' if the number is divisible by 3;
'buzz' if the number is divisible by 5;
'fizzbuzz' if the number is divisible by both 3 and 5.
'{number}' if the number doesn't fulfil any of the above conditions.
function fizzBuzz(number) {
if (number % 3 === 0) {
return "fizz"
};
if (number % 5 === 0) {
return "buzz"
};
if (number % 3 === 0 && number % 5 === 0) {
return "fizzbuz"
};
else return number
}
Try a little mental debugging. Look at your code and run different values through it in your mind:
What happens if you run the value 6 through it?
What happens if you run the value 10 through it?
What happens if you run the value 15 through it?
Ask yourself, "How could I fix this?" (hint: order of operations is important).
Something like this would do what you expect:
function fizzBuzz(n) {
const fizz = n % 3 ? '' : 'fizz' ;
const buzz = n % 5 ? '' : 'buzz' ;
return !fizz && !buzz ? '{number}' : `${fizz}${buzz}` ;
}
or this:
function fizzBuzz( n ) {
let s;
if ( n % 3 === 0 ) {
s = 'fizz' ;
if ( n % 5 === 0 ) {
s = 'fizzbuzz' ;
}
} else if ( n % 5 == 0 ) {
s = 'buzz' ;
} else {
s = '{number}' ;
}
return s;
}
This does [at most] 2 divisions and 2 comparisions.
The former does 2 divisions and 3-4 comparisons, so it is nominally less efficient.
But I know which version I'd rather look at.
The problem is, if a number is divisible by 5 and 3 (ex 15) "fizz" would only be returned (as a factor of 3) because every block {...} terminates the function with a return (this technique is called a "short-circuit", which isn't a bad practice). So you need to put the 5 and 3 condition first. Also that condition had an undefined variable called solution so that would've been the first error. One minor thing is that each block {...} was suffixed with a semi-colon: ; which is just bad formatting but it doesn't affect functionality.
function fB(number) {
if (number % 3 === 0 && number % 5 === 0) {
return "fizzbizz";
}
if (number % 3 === 0) {
return "fizz";
}
if (number % 5 === 0) {
return "bizz";
}
return number;
}
console.log(fB(15));
console.log(fB(575));
console.log(fB(49));
console.log(fB(51));
console.log(fB(80));
console.log(fB(375));
console.log(fB(99));
console.log(fB(Infinity));
I am building out a simple express app to solve the FizzBuzz question and I have been told to "Write a server using a framework that you are familiar with that accepts a GET request in the form of
GET http://localhost:3000/fizzbuzz/N
Where N is any number up to five digits.
How do I limit the number being passed in to 5 digits?
This is what I have so far ..
app.get('/fizzbuzz/:n', (req, res) => {
// request params for number being passed in
let fizzbuzz = [`${req.params.n}`];
let result;
// use for loop to iterate through number passed in and return proper response
for (let i = 1; i < 99999; i++) {
if (fizzbuzz % 3 === 0) {
if (fizzbuzz % 5 === 0) {
console.log('FizzBuzz');
result = 'FizzBuzz';
} else if (fizzbuzz % 3 === 0) {
console.log('Fizz');
result = 'Fizz';
}
} else if (fizzbuzz % 5 === 0) {
console.log('Buzz');
result = 'Buzz';
} else {
console.log(i);
}
}
res.send(result);
});
You can't actually limit the passed number in the url, but you could actually add a condition so if the number is more than 5 digits it will send an error or something like that.
Here's a solution:
You need actually to check if req.params.n is an integer, you can actaully use try/catch and parseInt() but i prefer to use string.matches("[0-9]+") so if it returns true that means its an integer.
After checking the first condition, just check string.length if its bigger than five then you should return an error or display a message to the user saying that he exceeded the limit.
To check if n is greater than five digits, you can just check if it is greater than 99999. To do this check, you could add some type of validation middleware like express-validator or just do an if and return a 400 or 422 error yourself.
Additionally, I am assuming any question like this would want you to return a string where each line is the number, fizz, buzz, or fizzbuzz, not the single value for the last number.
As just a fun idea since you have a max value, it wouldn't take up that much space to just pre-calc all the fizzbuzz values and store them in your database and turn this whole route into a really simple db query.
app.get('/fizzbuzz/:n', (req, res) => {
if(req.params.n > 99999){
res.status(422).send({
message: 'Invalid parameter must be 5 digits or under'
});
}
let result = '';
for (let i = 1; i <= req.params.n; i++) {
let line =''
if (i % 3 === 0) {
if (i % 5 === 0) {
line = 'FizzBuzz';
} else if (i % 3 === 0) {
line = 'Fizz';
}
} else if (i % 5 === 0) {
line = 'Buzz';
} else {
line = i;
}
result += line+'/n';
}
res.send(result);
});
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]);
In the book Eloquent JS in the section of recursion, a program was given:
Consider this puzzle: by starting from the number 1 and repeatedly
either adding 5 or multiplying by 3, an infinite amount of new numbers
can be produced. How would you write a function that, given a number,
tries to find a sequence of such additions and multiplications that
produce that number? For example, the number 13 could be reached by
first multiplying by 3 and then adding 5 twice, whereas the number 15
cannot be reached at all.
I have following program which look like checks it, but I don't know how to make it print he sequence.
function tester (value, key) {
if (value == key) {
return 1;
}
else if (value > key) {
return 0;
}
else {
if ( tester(value+5, key) || tester(value*3, key) ) {
return 1;
}
return 0;
}
}
Your version is a little odd to me, returning 1 or 0 rather than true or false. Are you mostly used to a language that conflates booleans with such integers? But it looks like it should work.
I would write it a bit differently. I generally prefer my recursion to count down to smaller inputs. You can write a simple function to test the values like this:
const m3a5 = (n) => n < 1
? false
: n == 1
? true
: m3a5(n - 5) || (n % 3 === 0 && m3a5(n / 3))
console.log(m3a5(13)) //=> true
console.log(m3a5(15)) //=> false
console.log(m3a5(18)) //=> true
This should be entirely equivalent to yours, modulo the boolean/int differences.
With this one, you can can then expand it in a fairly straightforward manner to allow you to capture the steps:
const m3a5 = (n, steps = []) => n < 1
? false
: n == 1
? steps
: m3a5(n - 5, ['+5'].concat(steps))
|| (n % 3 === 0 && m3a5(n / 3, ['*3'].concat(steps)))
console.log(m3a5(13)) //=> ['*3', '+5', '+5']
console.log(m3a5(15)) //=> false
console.log(m3a5(18)) //=> ['*3', '+5, '+5', '+5']
Note that this will show one possible path, not all of them. For instance ['+5', '*3'] is another possible result for m3a5(18), one which you would get by switching the main branch to
: (n % 3 === 0 && m3a5(n / 3, ['*3'].concat(steps)))
|| m3a5(n - 5, ['+5'].concat(steps))
But if you want all the paths, that would be significantly different code.
You could store the sequence and if found the calculation return the sequence.
function tester(key, value = 1, sequence = value) {
if (value > key) {
return false;
}
if (value === key) {
return sequence;
}
return tester(key, value + 5, '(' + sequence + ' + 5)')
|| tester(key, value * 3, sequence + ' * 3');
}
console.log(tester(15));
console.log(tester(11));
console.log(tester(24));
console.log(tester(37));
I'm working through codacademy and I can't understand the help discussions in the forum.
This is what I have so far but it returns false when i run the function with an even number:
var isEven = function(number) {
if (isEven % 2 == 0){
return true;
}else{
return false;
}
};
You are performing the mathematical operation on isEven (the function itself). You need to check number:
var isEven = function(number) {
if (number % 2 === 0) {
return true;
} else {
return false;
}
};
or better yet:
var isEven = function(number) {
return number % 2 === 0;
};
You could even do this, by making use of the truthy/falsy behavior of 1 and 0:
var isEven = function(number) {
return !(number % 2);
};
but I think the previous approach more clearly conveys how the logic works.
you can write this function as follows:
var isEven = function(number) {
return ((number % 2) == 0);
}
Your function is not working because you're checking against the function name, not the function parameter (number). Try this:
var isEven = function(number) {
return number % 2 == 0;
}
You doing it wrong in isEven itself..
Do it like..
var isEven = function(number) {
return number% 2 == 0;
}
Consider the following:
0 is considered to be false in Javascript.
1 is considered to be true in Javascript.
! makes false become true and vice versa.
The % operator can be used to keep higher numbers in a zero-to-one range.
Since 0 % 2 gives 0 (i.e. false) and 1 % 2 gives 1 (i.e. true), you simply need to invert the result with !:
function isEven(x) { return !(x % 2); }
console.log(isEven(0)); // true
console.log(isEven(1)); // false
console.log(isEven(2)); // true
console.log(isEven(3)); // false
Note that this could also be written as:
function isEven(x) { return (x % 2) == 0; }
...because:
0 % 2 is 0 (so it is true to say that it is equal to zero).
1 % 2 is 1 (so it is false to say that it is equal to zero).