Table-2 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".
- Variants to try above
- name field starts with "Ab", "A", "a" (lower case), "Z", "Za" (each in turn)
- name field ends with "z", "ly", "la" (each in turn)
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 CS101 code because they are so useful. These two functions are common in other computer languages.)
Solution code: