-3

I use this function to check the bottom part of my table cell and then later change its color.

function checkBottom(colIndex) {
  var colorReport = returnColor(5,colIndex);
  for (var row = 5; row > -1; row--) {
    colorReport = returnColor(row,colIndex);
    if(colorReport==='rgb(128,128,128)'){
      return row;
    }
  }
}

It doesn't do anything unless I change the if part to: colorReport==='rgb(128, 128, 128)'

What's different between rgb(a,a,a) and rgb(a, a, a)?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • 2
    Why should the comparison of strings ignore spaces? o.O – Andreas Aug 27 '21 at 14:07
  • 1
    What's different between "AA" and "A A" - are you saying you can't see the difference? – freedomn-m Aug 27 '21 at 14:07
  • That's just how strings behave. If you try to compare `"marc oriley" === "marco riley"` they won't match because they're different strings (in this case different names) – slebetman Aug 27 '21 at 14:09
  • 1
    Your real solution would to be not to check the colour - but instead add a `class` and check for the existence of that class. Then if you want to change the colour in future, you can do it in the css instead of your code (and then have the issue where you forget to change it in n-1 of the n places you've randomly added it...) – freedomn-m Aug 27 '21 at 14:12
  • @Andreas so you are sayin in system it saves like this: rgb(a, a, a)? and for comparison I should use this style? – Pooria Marofi Aug 27 '21 at 14:21
  • No. Where did I say/mention something like that? I have no idea what `returnColor()` does. Maybe that's just the way that function returns that value. Maybe its the way the browser returns that value... Don't compare colors like that. Instead use a class as suggested by @freedomn-m – Andreas Aug 27 '21 at 14:23
  • *Assuming* your `returnColor` gets the css value of the cell, each browser may return it differently, eg with/without a space, maybe as an `argb` or even using the colour name "lightgrey" or similar. It's not guaranteed to return in a specific format. Of course, we have no idea what `returnColor` actually does, maybe it *does* look for a class and then return a hardcoded string.... who knows? (well I *guess* OP does, but that's only a guess). – freedomn-m Aug 27 '21 at 14:28
  • @freedomn-m can you help me with a refrence how should I make that happen? – Pooria Marofi Aug 27 '21 at 14:32
  • You haven't provided enough information to provide a solution. If you could create a [mcve] in a snippet (edit and click `[<>]`) then a solution could easily be provided. To hazard some assumptions: where you "set its colour" instead of `style=color:red` use `class="active"` then your "checkBottom" would be a one liner `return $("tr.active").last()`. I suggest you ask a new question along the lines of "how do I use classes to find the last row" and include some html. Changing *this* question to that would be too much of a change from comparing strings with/without space. – freedomn-m Aug 27 '21 at 14:36
  • 1
    To fix *this* question, change your `returnColor` from `return color;` to `return color.replace(/\s/g, '');` - adjust for whatever is in returnColor of course - then compare without spaces. – freedomn-m Aug 27 '21 at 14:37
  • Hi and welcome to stackoverflow. The behaviour of the `===` test in javaScript is well-documented, and it is reasonable of people to expect you to give it a read before you ask for their help. In future, please make sure you read the relevant documentation before posting a question. If you still have a question after you've read the docs please post a question that includes a note about the research you've done. For details about how much research is expected, please see this post: https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users – morric Aug 29 '21 at 00:11

1 Answers1

-1

If you're some kind of space-ist who hates spaces, you can ignore them like this:

if(colorReport.replace(/\s/g, '') ==='rgb(128,      128,    128)'.replace(/\s/g, '')){
 //...
}

This uses the string's replace method to remove all whitespace.

You can make a function out of it, if you need to:

function compareStrings(str1, str2){
   return str1.replace(/\s/g, '').toLowerCase() === str2.replace(/\s/g, '').toLowerCase()
}

if(compareStrings(colorReport, 'rgb(128, 128, 128)')){
   // ...
}

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116