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.
Related
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
I have an if statement that has to allow for 4 parameter values and I am having trouble getting the syntax correct.
I have tested the 2 parts of the OR statement below separately and these return a true and false respectively but what I want is because the first argument returned a true for the do something to occur.
var d = 4;
var t = 2;
if ( (d >= 3.5 && d < 6) || (t >= 3.5 && t < 6) ){
console.log('true'); // do something
} else {
console.log('false');
}
I expect the if statement to be TRUE because the first part of the OR statement is correct and any code within to run.
UPDATE-------------------------------------------
Still getting issues, I have expanded the sample code to include what I am seeing
var dlG = "2";
var dl = "3.5";
if(parseFloat(dlG) < 3.5){
console.log("DLG Red");
}else if ( parseFloat(dlG) >= 3.5 && parseFloat(dlG) < 6 ){
console.log("DLG Purple");
}else if (parseFloat(dlG) >= 6){
console.log("DLG Green");
}
if(parseFloat(dl) < 3.5){
console.log("dl Red");
}else if ( parseFloat(dl) >= 3.5 && parseFloat(dl) < 6 ){
console.log("dl Purple");
}else if (parseFloat(dl) >= 6){
console.log("dl Green");
}
if((parseFloat(dl) < 3.5) || (parseFloat(dlG) < 3.5)){
console.log("Both Red");
}else if ((parseFloat(dl) >= 3.5 && parseFloat(dl) < 6 ) || (parseFloat(dlG) >= 3.5 && parseFloat(dlG) < 6 )){
console.log("Both Purple");
}else if ((parseFloat(dl) >= 6) || (parseFloat(dlG) >= 6)){
console.log("Both Green");
}
Essentially the first and second if statements work independently but I wanted to combine them with an OR statement in the 3rd if statement where I would expect to see 'Both Purple' in the console log but I see 'Both Red'.
Any ideas what I need to change?
I was approaching the problem from the wrong angle, on my example above the first if statement returned TRUE on certain criteria. On the second if statement the middle part (the first else if) returned TRUE on certain criteria.
I could not work out while the 3rd if statement was not working as expect but it was because the first and second parts both were returning TRUE.
Approaching the problem (in my larger code set) without using the if statements (and else parts) but using a different methodology has resolved the issue.
Thank you Randy and Colin for your help
I'm having some frustration with this code. I may not be seeing it.
I keep getting either an "Unexpected Token" or "ILLEGAL" error (the latter which completely preplexed me, considering I've never seen an error like that before in my life.
I've been double checking the syntax and I'm thinking it may be something I'm just not catching?
function fizzBuzz(n) {
2 for (i = 0; i<=n; i++) {
3 if (n%3===0) {
4 print ("fizz")
5 }
6 if (n%5===0) {
7 print ("buzz")
8 }
9 if (i%3 !== 0 && i%5 !== 0) {
10 return [i];
11 }
12 }
13 }
14
15
16 fizzBuzz(100);
I'd be thankful for the help! <3
You need some changes:
remove line numbers,
check first for 'fizz buzz' value and use console.log for output. Then use continue statement for jumping to top for next loop,
use i instead of n for checking.
function fizzBuzz(n) {
for (i = 0; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("fizz buzz");
continue;
}
if (i % 3 === 0) {
console.log("fizz");
continue;
}
if (i % 5 === 0) {
console.log("buzz");
continue;
}
console.log(i);
}
}
fizzBuzz(100);
function fizzBuzz(n) {
2 for (i = 0; i<=n; i++) {
3 if (n%3===0) { You're checking if the maximum number (n) is divisible by 3.
4 print ("fizz") You should be checking i.
5 }
6 if (n%5===0) { You're checking if the maximum number (n) is divisible by 5.
7 print ("buzz") You should be checking i.
8 }
9 if (i%3 !== 0 && i%5 !== 0) {
10 return [i]; You're returning an array with a single element: i.
11 } You should print i instead.
12 }
13 }
14
15
16 fizzBuzz(100);
The line numbers are what's causing the "ILLEGAL" error. Remove them.
print in JavaScript opens the print dialog to literally print paper out of a printer. Use console.log
If a number is divisible by both 3 and 5, fizzbuzz should be printed. Your code prints fizz and buzz on separate lines.
Working version ( https://www.youtube.com/watch?v=QPZ0pIK_wsc ) :
function fizzBuzz(n) {
for (var i = 0; i <= n; i++) {
var printVal = "";
if (i % 3 === 0) {
printVal += "fizz";
}
if (i % 5 === 0) {
printVal += "buzz";
}
if (printVal === "") {
printVal = i;
}
console.log(printVal);
}
}
fizzBuzz(100);
Lets decrease number of if statements, I have seen many fizzbuzzs - I would like to try another approach
const fizzBuzz = (i = 0, ret) => {
while (++i, ret = '', i <= 100) {
if (i % 3 === 0) {
ret += `fizz`
}
if (i % 5 === 0) {
ret += `buzz`
}
console.log(ret || i)
}
}
fizzBuzz()
The most important part is console.log(ret || i) - empty string is falsy value, so console will log ret if there is some value or current i value
If you're trying to print to your browser's console, use console.log() instead of print(), since print() will open the printer preview in a browser. I'm using Chrome. Remember to check the console by pressing F12 and clicking on "console" in case you've created a html file that includes your javascript script.
Since you are iterating i until it reaches the value of n, you if statements should look like:
if(i%3===0){
// your code...
}
Based on your code, it seems like you want to check for a value that is not a multiple of 3 && a multiple of 5. However, the fizzbuzz challenge requires to print fizzbuzz when encountering a number that is both a multiple of 3 && a multiple of 5. So, you'll need to add the following condition:
if (i%3 === 0 && i%5 === 0) {
console.log("fizzbuzz");
continue; /* Continue may or may not be necessary depending on your program's logic. Continue allows to re-enter the loop
without checking for the remaining if statements. If you use continue, this condition must be the first if statement inside the loop */
}
Your function doesn't need to return anything, since you're using console.log() to visualize the values in each loop iteration. That's why you need to change return [i] for console.log(i).
Don't give up and keep trying!
If you have further questions, I'll be glad to help you.
So what i need to do in this task is to print out the numbers from 1-20. And the code should also fulfill the rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the
console.
Otherwise, just print out the number.
for ( var i = 0 ; i < 20 ; i++) {
if ( i % 3) {
console.log("Fizz");
}
else if( i % 5) {
console.log("Buzz");
}
else if(i % 3 || 5) {
console.log("FizzBuzz");
}
else {
console.log(i);
}
}
The error i am getting :"You printed FizzBuzz when you should have printed 1"
You have some errors in your code
for (var i = 1; i < 21; i++) { // needs to start with 1
// You should check this condition first
if (i % 3 == 0 && i % 5 == 0) { //needs '==' and '&&' operator
console.log("FizzBuzz");
} else if (i % 3 == 0) { // you need to check for equality to zero
console.log("Fizz");
} else if (i % 5 == 0) { // here too, needs '=='
console.log("Buzz");
} else {
console.log(i);
}
}
A one line solution
for(i=1;i<=20;i++)console.log((!(i%3)?'Fizz':'')+(!(i%5)?'Buzz':'') || i);
Tests for matches using two conditional (ternary) operators. The results are concatenated, eliminating the need for a third "matches both" test. A logical OR is used to print the index when the string result is empty, i.e., no matches. A single console statement outputs the final result.
As for the errors in OP's code, #elclanrs already pointed out the problem in a comment. else if(i % 3 || 5) is incorrect (suggest printing the result in the console to see why).
Run the snippet to try
// Here we output to the screen rather than the console
for(i=1;i<=20;i++)window.stdout.innerHTML+='<li>'+((!(i%3)?'Fizz':'')+(!(i%5)?'Buzz':'')||i);
<ol id="stdout">
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