The question I'm trying to solve goes as follows:
I often struggle to know whether I should wear shorts or pants on a given day. Please help me decide by writing me a function called isShortsWeather
It should accept a single number argument, which we will call temperature (but you can name it whatever you want).
If temperature is greater than or equal to 75, return true.
Otherwise, return false
This exercise assumes temperature is in Fahrenheit
Expected result:
isShortsWeather(80) //true
isShortsWeather(48) //false
isShortsWeather(75) //true
The code I wrote is:
function isShortsWeather(temperature) {
if (temperature < 75); {
return false;
}
if (temperature >= 75) {
return true;
}
}
As snippet:
function isShortsWeather(temperature) {
if (temperature < 75); {
return false;
}
if (temperature >= 75) {
return true;
}
}
console.log(isShortsWeather(80)) //true
console.log(isShortsWeather(48)) //false
console.log(isShortsWeather(75)) //true
Please help me out by informing me what is wrong with my code and how should I fix this issue. I feel like I am relatively close to solving it. Thanks!
Simply return the boolean:
return temperature >= 75;
the main reason it's not working is because you have a extra ; after the first condition.
you can shorten the function body to one line
function isShortsWeather(temperature) {
return temperature >= 75;
}
you forgot ; at the line 2, if you remove it it will work. Also it would be better if you make the 2nd if statement in else if
function isShortsWeather(temperature) {
if (temperature < 75) {
return false;
} else if (temperature >= 75) {
return true;
}
I would recommend a return on both if statements and not a console log on false. You will use console.log to call the function. I have edited the code a bit as the semicolon on line 2 not needed.
function isShortsWeather(temperature) {
if (temperature < 75) {
return false;
} else {
return true;
}
}
temperature = 74;
console.log(isShortsWeather(temperature));
Below you will find the correct code that is working.
Hope answer your questions.
function isShortsWeather(temperature) {
if (temperature >= 75) {
return true;
}
else {
return false;
}
}
console.log(isShortsWeather(76));
console.log(isShortsWeather(74));
function isShortsWeather(temperature) {
if (temperature < 75); {return 0;}
if (temperature >= 75) {return 1;}
}
I think that will solve your problem
You can return the value instead of console logging it.
It would also be a good idea to refactor the "magic number" 75 into a variable with a default value, allowing you to easily change or override it later.
function isShortsWeather(temperature, cutoffTemp = 75) {
if (temperature < cutoffTemp) {
return false;
}
if (temperature >= cutoffTemp) {
return true;
}
}
console.log(isShortsWeather(81));
Related
I just made the jump from IDE to Node-RED where JavaScript comes into play. I am eager to learn but my knowledge is limited knowledge.
What I am trying to do, is simple to check the msg.payload, if the value is larger than 25 then I want to return 1 else I want to return 0.
I have tried different codes, but it does not work.
m = msg.payload;
if (m < 25)
{
return 1;
}
else if (m > 25)
{
return 0;
}
and
m = msg.payload;
if (m < 25)
{
return 1;
}
else ()
{
return 0;
}
For Node-RED function nodes you must return a msg object not just a value.
So the correct form for this test is as follows.
if (msg.payload > 25) {
msg.payload = 1
} else {
msg.payload = 0;
}
return msg;
You can do it in one line:
return msg.payload > 25 ? 1 : 0;
or if you just need booleans (true/false):
return msg.payload > 25;
Try this:
if (Number(m) > 25){
return 1;
}
else{
return 0;
}
You can simplify this a good bit, the else isn't even needed in this case:
if (m > 25) {
return 1;
}
return 0;
When the return inside the if statement is executed, the code that follows won't be run... making the else statement unnecessary.
The else is redundant when you're just returning like that, and you've got your comparison operators backwards. Right now it'll return 1 if it's less than 25, not greater than.
if (msg.payload > 25) {
return 1;
}
return 0;
You should learn comparision operator where > denotes greater than and < denotes lesser than , you can simplify using ternary operator as
return msg.payload > 25 ? 1 : 0;
DEMO WITH IF ELSE
function check(m){
if (m > 25){
return 1;
}
else
{
return 0;
}
};
console.log(check(50));
DEMO WITH TERNARY
function check(m){
return m > 25 ? 1 : 0;
};
console.log(check(50));
As you have mentioned that you want to return 1 if value is greater than 25. Please check that you have written wrong condition.
Your condition must be this:
if ( m > 25 ){
return 1;
} else {
return 0;
}
You are making a very silly mistake of less than and greater than sign. Hope it helps you.
m = msg.payload;
if (m > 25)
{
msg.payload=1;
}
else if (m < 25)
{
msg.payload=0;
}
return msg;
So I have been reading this book named: Eloquente JavaScript, and to be true somethings in this book seems quite complex. There was this challenge were I had to wright a function that showed true or false depending if the value was even or not. My version is quite shorter then the one from the book. What should be the best way of doing this? Also why did he do it like this?
Eloquente JavaScript code:
function isEven(n) {
if (n == 0)
return true;
else if (n == 1)
return false;
else if (n < 0)
return isEven(-n);
else
return isEven(n - 2);
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
My own code:
function even(num) {
if (num % 2 == 0){
return true;
}
else{
return false ;
}
};
console.log(even(17));
console.log(even(10));
console.log(even(-33));
console.log(even(-40));
function even(num) {
return num % 2 === 0;
}
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;
}
};
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.
Ok, I'm still a beginner at JavaScript, but I'm trying to write a rock-paper-scissors type game. Everything is working pretty well, except that every time that I run the "reload" function the output is whatever is the first "if" statement of the second if/else statement. By this I mean that the output "Both sides reload." would come back every time if the code were to be arranged how it is below. Also I already know that the randomizer works. Any help will be greatly appreciated. Thanks.
var reload = function() {
var random = function() {
var randomizer = Math.random() * 100
if (randomizer <= 33) {
var compDecision = 1
}
else if (randomizer > 67) {
var compDecision = 2
}
else if (33 < randomizer && randomizer <= 67) {
var compDecision = 3
}}
if (compDecision = 1) {
confirm("Both sides reload.")
}
else if (compDecision = 2) {
confirm("You reload and the enemy takes up defensive positions.")
}
else if (compDecision = 3) {
confirm("The enemy takes you out as you reload.")
}}
First of all use = for assignments, and == for logical compare operation.
Then you should declare var compDecision as a empty or default var and then assign to it a value without using var again. I would recommended using semi-colons ; to end your statements, unlike JavaScript, they are not optional for other languages.
Here is your working code, check the differences conclude the solution:
var reload = function () {
var compDecision = 0;
var random2 = function () {
var randomizer = Math.random() * 100
if (randomizer <= 33) {
compDecision = 1;
} else if (randomizer > 67) {
compDecision = 2;
} else {
compDecision = 3;
}
}
random2();
if (compDecision == 1) {
alert("Both sides reload.");
} else if (compDecision == 2) {
alert("You reload and the enemy takes up defensive positions.");
} else if (compDecision == 3) {
alert("The enemy takes you out as you reload.");
}
}
reload();
Tested here : http://jsfiddle.net/urahara/6rtt0w62/