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

4
  • 1
    Maybe you are bitten by the javascript equality goodness: stackoverflow.com/questions/359494/… name === employee might do the trick Commented Mar 23, 2013 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? Commented Mar 23, 2013 at 18:07
  • @rene equality is always truthy - what's true for === will always be true for ==. Commented Mar 23, 2013 at 18:09
  • @Kolink good point, didn't realize that Commented Mar 23, 2013 at 18:20

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

2 Comments

That seems to have done it!!! Thanks a million! Originally I had names[i] == employees [j]... why does the array[i] return another array?
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.
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])

Comments

0

Using toString method might help as well:

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

But the other suggestions look cleaner.

Comments

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.