Animalplanet "Cannot Read Property 'split' of Undefined"
If y'all've ever used JavaScript'southward split method, there's a good take chances that yous've encountered the following mistake: TypeError: Cannot read belongings 'separate' of undefined.
In that location are a few reasons why you would receive this mistake. Well-nigh likely it's but a basic misunderstanding of how dissever works and how to iterate through arrays.
For example, if you endeavor to submit the following code for the Find the Longest Word in a String challenge:
function findLongestWord(str) { for(allow i = 0; i < str.length; i++) { const array = str.split(" "); array[i].split(""); } } findLongestWord("The quick brown flim-flam jumped over the lazy dog"); it will throw the TypeError: Cannot read property 'split' of undefined error.
The split method
When split is called on a string, information technology splits the cord into substrings based on the separator passed in as an argument. If an empty cord is passed as an argument, split treats each character as a substring. It then returns an array containing the substrings:
const testStr1 = "Examination test 1 2"; const testStr2 = "cupcake pancake"; const testStr3 = "Starting time,Second,Tertiary"; testStr1.split(" "); // [ 'Examination', 'test', '1', '2' ] testStr2.split(""); // [ 'c', 'u', 'p', 'c', 'a', 'k', 'e', ' ', 'p', 'a', 'due north', 'c', 'a', 'k', 'due east' ] testStr3.divide(","); // [ 'Get-go', 'Second', 'Third' ] Check out MDN for more details about dissever.
The trouble explained with examples
Knowing what the split method returns and how many substrings you tin expect is the cardinal to solving this challenge.
Let's take another look at the code above and see why it's not working as expected:
part findLongestWord(str) { for(let i = 0; i < str.length; i++) { const assortment = str.split up(" "); array[i].split(""); } } findLongestWord("The quick brown fox jumped over the lazy dog"); Splitting str into an array like this (const array = str.split(" ");) works as expected and returns [ 'The', 'quick', 'brown', 'flim-flam', 'jumped', 'over', 'the', 'lazy', 'dog' ].
Just take a closer look at the for loop. Rather than using the length of array as a status to iterate i, str.length is used instead.
str is "The quick brown pull a fast one on jumped over the lazy canis familiaris", and if you log str.length to the console, you'll get 44.
The last statement in the trunk of the for loop is what's causing the error: array[i].carve up("");. The length of array is nine, so i would rapidly go way over the maximum length of assortment:
function findLongestWord(str) { for(let i = 0; i < str.length; i++) { const array = str.separate(" "); panel.log(assortment[i]); // array[0]: "The" // array[1]: "quick" // array[two]: "brown" // ... // array[nine]: "dog" // array[10]: undefined // array[11]: undefined } } findLongestWord("The quick chocolate-brown fox jumped over the lazy dog"); Calling array[i].split(""); to split each string into substrings of characters is a valid approach, but information technology will throw TypeError: Cannot read holding 'split' of undefined when information technology's passed undefined.
How to solve Observe the Longest Word in a String with dissever
Let's quickly get over some pseudo code for how to solve this problem:
- Split up
strinto an array of individual words - Create a variable to track the greatest word length
- Iterate through the array of words and compare the length of each discussion to the variable mentioned higher up
- If the length of the current word is greater than the 1 stored in the variable, replace that value with the electric current discussion length
- Once the length of every word is compared with the maximum word length variable, return that number from the part
First, split up str into an array of private words:
function findLongestWordLength(str) { const array = str.split(" "); } Create a variable to continue track of the longest word length and set it to zero:
function findLongestWordLength(str) { const array = str.split(" "); let maxWordLength = 0; } Now that the value of array is ['The', 'quick', 'brown', 'play tricks', 'jumped', 'over', 'the', 'lazy', 'dog'], you can use assortment.length in your for loop:
function findLongestWordLength(str) { const array = str.split(" "); permit maxWordLength = 0; for (permit i = 0; i < assortment.length; i++) { } } Iterate through the assortment of words and check the length of each give-and-take. Call back that strings likewise have a length method you can telephone call to hands go the length of a string:
function findLongestWordLength(str) { const array = str.split(" "); let maxLength = 0; for (let i = 0; i < array.length; i++) { array[i].length; } } Use an if statement check if the length of the current word (assortment[i].length) is greater than maxLength. If so, replace the value of maxLength with array[i].length:
function findLongestWordLength(str) { const array = str.split(" "); let maxLength = 0; for (let i = 0; i < array.length; i++) { if (array[i].length > maxLength) { maxLength = array[i].length; } } } Finally, return maxLength at the end of the function, after the for loop:
part findLongestWordLength(str) { const array = str.split up(" "); allow maxLength = 0; for (allow i = 0; i < array.length; i++) { if (array[i].length > maxLength) { maxLength = assortment[i].length; } } return maxLength; } Learn to code for complimentary. freeCodeCamp'southward open source curriculum has helped more than than 40,000 people go jobs every bit developers. Get started
Source: https://www.freecodecamp.org/news/cannot-read-property-split-of-undefined-error/
0 Response to "Animalplanet "Cannot Read Property 'split' of Undefined""
Post a Comment