-1

I'm currently learning javascript (by using Google Script currently), I feel like this is a simple mistake, but I can't figure out a way around it.

The code below works, if I set "var colour" to a colour code. But when I change it to an if statement I get the issue. I've tried a bunch of different formats and continued having the same issue...

For info it just gets a few rows from a spreadsheet and then formats the selected rows and displays it to the user.

//Example of part of working code:
  if(Line.length == 0){
  var Line = lines 
  .slice(1)
  .filter(function(row) { return row[4] == e.message.text;})
  .map(function(row) {
    var colour = if(row[6]=1){return "#ff0000"};
    return '<b>' + row[3] + '</b> ('+ row[1] + ' or ' +row[2] + ')' + '\n' + '<font color=' + colour + '>Region: ' + row[6] + "</font>";
  });
  } 
Hayden
  • 3
  • 3
  • 1
    We can't tell you why an error occurs if you don't present the code where the error occurs. It's like going to the doctor, and saying "My right leg hurts; here's an X-ray of my healthy left leg so you can see how it should be." :) If you literally just inserted that `if` where the comment is, either move it to a new line, or add a semicolon after `"#000000"`. – Amadan Mar 18 '20 at 09:51
  • I meant that if you replace the "#000000" with the comment (without the //) then you get an error. I've now changed what I wrote to be a bit clearer of the code that has an error... – Hayden Mar 18 '20 at 09:57
  • See, I had no idea that's what you meant. It wouldn't have even crossed my mind. This is why seeing the code where the error happens is important. :) Thanks for the edit. – Amadan Mar 18 '20 at 10:00
  • `var colour = if(row[6]=1){return "#ff0000"};` `var` expects an expression on the right-hand side, a statement is not allowed. – Teemu Mar 18 '20 at 10:01
  • Does this answer your question? [How to write an inline IF statement in JavaScript?](https://stackoverflow.com/questions/10270351/how-to-write-an-inline-if-statement-in-javascript) – VLAZ Mar 18 '20 at 10:03
  • 1
    You cannot use an `if` in this place. If you want it inline, then use the conditional operator `? :` or alternatively convert to a proper `if` and assign the value in the blocks. Also note that `row[6]=1` [is assignment, not comparison](https://stackoverflow.com/questions/11871616/in-javascript-vs/) – VLAZ Mar 18 '20 at 10:05
  • Thanks everyone! I actually did have (row[6] == 1) in my original code, I must have lost one when I tried to simplify the code to share here. Hopefully my next question here has a few less embarrassing mistakes – Hayden Mar 18 '20 at 10:23

1 Answers1

2

There is a difference between statements and expressions.

Just like you can put apples in a basket but not baskets in an apple, you can put expressions in a statement but not a statement in expressions.

var colour = ... is a statement that expects an expression on the right-hand side of the equals sign. You can't put an if statement there. You can use a conditional operator to make a a conditional expression:

var colour = row[6] == 1 ? "#ff0000" : "#000000";

Or you can use a full if statement to execute two variant assignment statements:

var colour;
if (row[6] == 1) {
  colour = "#ff0000";
} else {
  colour = "#000000";
}

Note also that return #ff0000 would have returned literally "#ff0000", not the text with the colour #ff0000 as I assume you want; also note that row[6] = 1 would assign 1 to row[6], not compare it.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks heaps! The difference is now quite obvious to me, I had never really had it explained to me like that. Cheers! – Hayden Mar 18 '20 at 10:18