As an App Academy student, you are provided with many programming resources, from videos and practice problems to live lectures and Q&As. Learn everything this curriculum has to offer and you're ready to succeed in a high-paying software engineering job! Yet, the information here still only represents a tiny fraction of all the existing coding knowledge in the world. What do you do if you need to know something that's not in the curriculum?
Fortunately, we have internet, which does contain a majority of the world's coding knowledge. At App Academy, we don't expect you to memorize the syntax and spelling of every single JavaScript method. Instead, we want you to learn how to think like a programmer. That means knowing how to use web search and official documentation to find answers quickly.
MDN (Mozilla Developer Network) is Mozilla's official collection of web development resources created and maintained by a community of active developers. The website is https://developer.mozilla.org/. We'll be referring to the JavaScript portion of the docs for this exercise.
Let's say you're working on a JavaScript function that finds the first instance of a character in a string and returns null if it doesn't exist. What do you do?
If you're working on a project, you can type "how do I find the first instance of a character in a string JavaScript" into Google. This is a pretty good option and will usually return good resources in the first few results. You do need to be careful with this approach because it may also return some faulty or incorrect results as well. If you do this, always read and test the code before blindly copy/pasting into your project.
A safer option would be to search through the official MDN documentation. Typing "string" into the search bar will show a list of results related to strings. Click on the one labeled JavaScript. From here, you can scroll through the built-in methods until you find one that accomplishes your task.
You've seen indexOf() which "returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found." Sounds promising! The page also includes demos which show the code in action. This should be enough for you to complete this task:
// Return the index of the character
function findCharacterInString(str, chr) {
const characterIndex = str.indexOf(chr);
return characterIndex;
}
Occasionally, you'll have to write code without access to documentation. This happens very rarely in the real world but does come up in job interviews. If you find yourself in this situation and you forget some syntax, don't panic! Be honest and tell your interviewer, "I always forget the name of the method to find a character in a string... Is it findChar()?" Usually, your interviewer will either tell you the name of the answer or say it's fine because they forgot too. Even experts can't remember the names of all of the available string methods.