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>";
});
}
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.