1

I am trying to make a program that organizes punch-ins from an RFID scanner in google sheets...

When I go to match scan [i] with employee name [j] it never succeeds.

I have a simple if statement:

//names[i] is the name of the card for the scan event (card names = employee full name)
var name = names[i];

//the j for loop will check all employee names in the list for a match
var employee = employees[j];
if (name==employee) {
  var ifsuccess = true;
}

But I never get ifsuccess to = true... it may be obvious but I have never done programming in google script before (or javascript :P) does anyone know what I've done wrong?

screenshot

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 1
    Maybe you are bitten by the javascript equality goodness: http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use name === employee might do the trick – rene Mar 23 '13 at 18:03
  • 2
    @rene If `a == b` never succeeds, what makes you think that `a === b`, a more specific conditional, would do any better? – Niet the Dark Absol Mar 23 '13 at 18:07
  • @rene equality is always truthy - what's true for === will always be true for ==. – Barney Mar 23 '13 at 18:09
  • @Kolink good point, didn't realize that – rene Mar 23 '13 at 18:20

4 Answers4

1

It looks like you're comparing two Array objects rather than two strings. The screenshot claims that name and employee are indexed arrays with a single element.

To compare the string data inside each Array object:

name[0] == employee[0]     // equal value

Or the slightly safer:

name[0] === employee[0]    // equal value and of the same type
Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59
  • That seems to have done it!!! Thanks a million! Originally I had names[i] == employees [j]... why does the array[i] return another array? – user2202843 Mar 23 '13 at 18:10
  • Because names and employees are arrays of arrays. Each array element in names and employees is an array with a single element (a string). In other words, they're arrays of arrays of strings, rather than arrays of strings, which you might typically expect. – Matt Coughlin Mar 23 '13 at 18:15
0
> foo = ['foo'];
[ 'foo' ]
> bar = ['foo'];
[ 'foo' ]
> foo == bar
false
> foo === bar
false

Comparing array to array- even if they look the same, will not be true unless they are really referencing the same array in memory.

> bar = foo;
[ 'foo' ]
> foo == bar
true
> foo === bar
true

So, try comparing strings instead:

if (bar[0] === foo[0])
Spencer Lockhart
  • 1,164
  • 7
  • 7
0

Using toString method might help as well:

name.toString()==employee.toString()

But the other suggestions look cleaner.

guessimtoolate
  • 8,372
  • 2
  • 18
  • 26
0

In your debugger you can see that both name and employee refer to arrays with a string in it - yet those are two distinct arrays (with different internal ids), which are therefore not equal in terms of ==.

If you want to compare the strings inside them, use

var ifsuccess = name[0] == employee[0];
Bergi
  • 630,263
  • 148
  • 957
  • 1,375