String startsWith endsWith

Previously we did tests with == and < -- in this short section, add the startsWith/endsWith functions which test which letters are at the start or end of a string.

These tests work very well with the name strings pulled out of the baby data. Here we can look at all the names beginning with "Ab".


For our purposes, strings support a s.startsWith("Ab") function, here testing if the string in the variable s starts with the "Ab" .. true or false. Likewise, there is s.endsWith("yz"), here testing if the string in variable s has "yz" at its very end. (Sadly, these two functions are not part of standard JavaScript; I made them work just for out code because they are so useful. These two functions are common in other computer languages.)

Solution code:

If logic inside the loop:

if (row.getField("name").startsWith("Ab")) {
  print(row);
}
// Change string to "A", "a", "Z", .. each in turn

if (row.getField("name").endsWith("z")) {
  print(row);
}

> exercises