Codewars: SyntaxError unexpected token '&&' in a If else statement - javascript

Trying to solve Opposites Attract kata with an if else statement to check if flower1 has even/odd pedals as well as flower2 using &&.
if ((flower1 % 2) == 1) && ((flower2 % 2) == 0){
return true;
Which gives a SyntaxError: unexpected token '&&'

if( ((flower1 % 2) == 1) && ((flower2 % 2) == 0) ){
return true;
}
You did it correctly just didn't have enough brackets

Related

Why does my test pass, even though it doesn't meet my logic gate?

I'm working on telephone validator on FCC. For some reason this passes 5555555555. Why does my logic gate pass this number? For context, this isn't my first attempt at this code. I've added multiple statements, nested if statements, and it still doesn't catch it. Why does this evaluate to true? Here's the code:
function telephoneCheck(str) {
if(str[0] === '1' || '(' && str.length >= 10) {
return true;
}
else {
return false;
}
}
telephoneCheck("5555555555");
You need to restate the condition you're comparing (|| '(' will always be true):
if(str[0] === '1' || str[0] === '(' && str.length >= 10) {
This is due to the fact that && has a greater precedence than the || operator. So without the parenthesis, the '(' && str.length >= 10 part of the expression is evaluated first. So the ultimate condition becomes str[0] === '1' || true which would always be true. So your code would return true for any string of length >= 10

If statement causes: Uncaught SyntaxError: Unexpected token <

I am trying to randomly select an option using Math.random()
This is the code I have so far.
function randChoice() {
var chance = Math.floor(Math.random() * 100);
if (chance > -1 && < 50) {
console.log("Option 1"); // 1
} else if (crateId > 49 && < 71) {
console.log("Option 2"); // 2
} else if (crateId > 70 && < 91) {
console.log("Option 3"); // 3
}
}
When I run it, I get an error saying Uncaught SyntaxError: Unexpected token <.
What is wrong with my syntax? I've been looking for at least an hour, but I can't find anything that will help or any indication of what went wrong.
This crateId > 49 && < 71 is not valid syntax.
There needs to be either a variable name or a literal between any two binary operators, only unary operators may be adjacent.
It needs to be: crateId > 49 && crateId < 71.

javascript two conditions in if statement

I'm trying to write code that checks two conditions in an if statement but no luck.
for(var num = 1; num<=100;num +=1)
{if (num % 3==0) and (num % 5==0) console.log("fizzbuzz");
else if (num % 3==0) console.log("fizz");
else if(num % 5==0) console.log("buzz");
else console.log(num);}
First, change and to &&,
Also, your if statement does not have parenthesis like that:
for(var num = 1; num<=100;num +=1){
if ((num % 3==0) && (num % 5==0)) console.log("fizzbuzz");
else if (num % 3==0) console.log("fizz");
else if(num % 5==0) console.log("buzz");
else console.log(num);
}
Then it should work like a charm!
You need to work your indentation too.
Use && instead of and.
for (var num = 1; num <= 50; num += 1) {
if (num % 3 == 0 && num % 5 == 0) console.log("fizzbuzz");
else if (num % 3 == 0) console.log("fizz");
else if (num % 5 == 0) console.log("buzz");
else console.log(num);
}
Indentation. If you use IDE (like webstorm), there should be an option to auto format the code. FYI: indentation plays a very big role in understanding your own code in some time (for example, right after you write it:))
I wouldn't call it js becase there's logical AND operator: &&, not and. At first glance it can be even treated as a portion of pseudocode, bad pseudocode
if statement has structure like if (condition) operator, so when you want to check multiple conditions, you should wrap them with parenthesis: if (condition1 && condition2 && ... && conditionN). All the conditions are finally equvalent to a single condition that's checked after evaluation.
Of course, if statement allows you not to wrap operators with curly brases: if (condition) some operator;. But it's more readable and understandable if you write if (condition) {operator}. For example, when you change your code and want to add some more operators, it will be more secured from some confusing mistakes. Let's say you want to write smth like this:
if(everythingIsOk)informUser;
Let's say you'd like to add a message if everything is ok. You could write this:
if (everythingIsOk)
informUser;
writeMessage;
In the example above you make the program to write the message anyway because it's a separate instruction. If you wrote initially if (everythingIsOk){informUser} it would be easier to just add one more instruction that must be executed in this particular case. More than that, it's more readable.
You would probably be surprised, but the following is valid too:
if (everythingIsOk);
The following is more readable because at least it makes people think that you haven't forgotten about your construction somewhere in the middle:
if (everythingIsOk){}
And finally, if you want to emphasize that nothing should be done in some cases, that's the approach (just fyi):
if (everythingIsOk){/* Do nothing, no code here is not a mistake! */}
So your code would be more readable if you wrote it like this:
for (var num = 1; num <= 100; num += 1) {
if ((num % 3 == 0) && (num % 5 == 0)) {
console.log("fizzbuzz");
} else if (num % 3 == 0) {
console.log("fizz");
} else if (num % 5 == 0) {
console.log("buzz");
} else {
console.log(num);
}
}
I'd recommend you read at least examples you learn more accurately.

How to solve a *missing ;* in a for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Good afternoon,
The code below should represent the FizzBuzz game.
for (var i = 0, i < 100, i++) {
if(((i % 3) == 0) && ((i % 5) = 0)) {document.write('FizzBuzz')}
else if( ((i % 3) == 0) && ((i % 5) != 0)) {document.write('Fizz')}
else if( (( i % 3 ) != 0) && ((i % 5) == 0) ) {document.write('Buzz')}
else {document.write(i)}
}
This is the error I got in Mozilla Firefox Debugger
SyntaxError: missing ; after for-loop initialize 1.18.
I'm stuck.
There are two possible mistakes
1) Inside for loop conditional statement it should be ; but ,
2) There is a invalid left side assignment at ((i % 5) = 0), it should be ((i % 5) == 0)
To debug such issue use any linter and properly indent the code
for (var i = 0; i < 100; i++) {
if (((i % 3) == 0) && ((i % 5) == 0)) {
document.write('FizzBuzz')
} else if (((i % 3) == 0) && ((i % 5) != 0)) {
document.write('Fizz')
} else if (((i % 3) != 0) && ((i % 5) == 0)) {
document.write('Buzz')
} else {
document.write(i)
}
}
DEMO
You should use semi colons instead of commas in your loop:
for (var i = 0; i < 100; i++)
{
if ( i % 3 == 0 && i % 5 == 0 ) {document.write('FizzBuzz')}
else if ( i % 3 == 0 && i % 5 != 0 ) {document.write('Fizz')}
else if ( i % 3 != 0 && i % 5 == 0 ) {document.write('Buzz')}
else {document.write(i)}
}
You also have a massive parentheses overload! You don't need that many parens, they were also causing you errors.

JavaScript If Statement + Uncaught TypeError

1. I want ratingClass = 'fresh' if rating > 59 OR if audience_rating > 70. How?
I tried with
if (rating > 59) || (audience_rating > 70) {
var ratingClass = 'fresh';
Here's the code:
if (rating > 59) {
var ratingClass = 'fresh';
} else if (rating > 0){
var ratingClass = 'rotten';
} else {
var ratingClass = 'na';
}
if (audience_rating > 59) {
var audienceClass = 'fresh';
} else if (audience_rating > 0){
var audienceClass = 'rotten';
} else {
var audienceClass = 'na';
}
$parentEl.addClass(ratingClass);
2. In line 114 of http://pastebin.com/UN8wcB7b I get Uncaught TypeError: Cannot read property 'length' of undefined every ~3 seconds when hideRotten = true. Is it easily fixed and/or do I need to worry about it at all?
I am new to JavaScript coding, currently I am trying to learn by doing. Can you recommend any resources to learn writing Chrome Extensions with JavaScript?
Thanks :-)
it's because you are trying to read the length of a null element. So put a condition to test if movies are null in your if statement on line 114 so it says something like
if(data.movies && data.movies.length > 0)
Though if you're setting some data in this if statement that will be used other places in the code you may have to put checks like this in other places as well to completely avoid this type of problems.
The error definitely means
typeof data.movies === "undefined"
to avoid this I will recommend
...
$.getJSON(movieUrl, function(data){
// data can be undefined becoz of various reasons and so is data.movies
if(!(typeof data === "undefined") && !(typeof data.movies === "undefined")) {
//put similar checks in ur code
...
1) the condition after the if must always be completely surrounded in brackets:
// wrong
if (rating > 59) || (audience_rating > 70) {
// has to be:
if ( rating > 59 || audience_rating > 70 ) {
or if you are unsure about the operator precedence:
if ( (rating > 59) || (audience_rating > 70) ) {
2) You have to check first, if the movies attribute exists in your data respone (Because if it doesn't, you also can't call length on it):
// can throw error if data.movies === undefined
data.movies.length > 0
// the safe way, check data.movies first:
if (data.movies && data.movies.length > 0)
this is pretty much equivalent to the long version*:
if (typeof(data.movies) === `undefined` && data.movies.length > 0)
* Not exactly, read this article why

Categories