Snippets Collections
// This array (characters) has a length of 4 i.e characters.length is 4
// characters[0] will return ["a", "b", "c"]
// characters[0][1] will return "b"
// and so on.
const characters = [
  ["a", "b", "c"],
  ["d", "e", "f"],
  ["g", "h", " i"],
  ["x", "y", "z"],
];

function characterExist(letter) {
  // we initialize exists to false because we intend to set it to true only if /// we find the letter in the colors array
  let exist = false;

  // TODO(1): Write code to loop through the characters array, and set exist to
  // true if the value in the variable letter is found in the array.
    for(i=0; i<=characters.length; i++){
        for(j=0; j<=characters[i].length; j++){
            if(characters[i][j] === letter){
                exist = true;
        return true
            } 
        }
    }
    return exist;
}

// THIS IS FOR TESTING ONLY
console.log("a exists = " +  characterExist("a")); // prints true
console.log("p exists = " +  characterExist("p")); // prints false
star

Wed Jun 23 2021 13:58:24 GMT+0000 (Coordinated Universal Time) edconnect.com

#2-darrays #loop

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension