Cannot Read Property of Undefined Mocha React
If yous've ever used JavaScript's split
method, there'south a good risk that you've encountered the following mistake: TypeError: Cannot read holding 'split' of undefined
.
There are a few reasons why yous would receive this error. Most likely it'south just a bones misunderstanding of how split
works and how to iterate through arrays.
For example, if you effort to submit the following code for the Find the Longest Word in a String challenge:
function findLongestWord(str) { for(let i = 0; i < str.length; i++) { const array = str.split up(" "); assortment[i].separate(""); } } findLongestWord("The quick brown play a joke on jumped over the lazy dog");
it will throw the TypeError: Cannot read belongings 'separate' of undefined
error.
The separate
method
When split
is chosen on a cord, it splits the string into substrings based on the separator passed in as an argument. If an empty string is passed equally an argument, divide
treats each graphic symbol as a substring. It then returns an assortment containing the substrings:
const testStr1 = "Test test 1 2"; const testStr2 = "cupcake pancake"; const testStr3 = "First,2d,Tertiary"; testStr1.split(" "); // [ 'Test', 'exam', '1', 'two' ] testStr2.separate(""); // [ 'c', 'u', 'p', 'c', 'a', 'thou', 'e', ' ', 'p', 'a', 'due north', 'c', 'a', 'k', 'due east' ] testStr3.split(","); // [ 'First', 'Second', 'Third' ]
Check out MDN for more than details well-nigh separate
.
The trouble explained with examples
Knowing what the split up
method returns and how many substrings you lot can expect is the cardinal to solving this challenge.
Allow's take another look at the code above and see why information technology's not working as expected:
function findLongestWord(str) { for(permit i = 0; i < str.length; i++) { const array = str.split(" "); array[i].split(""); } } findLongestWord("The quick dark-brown fox jumped over the lazy dog");
Splitting str
into an assortment similar this (const array = str.split(" ");
) works every bit expected and returns [ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'canis familiaris' ]
.
Only have 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 chocolate-brown fox jumped over the lazy dog", and if you lot log str.length
to the console, y'all'll get 44.
The last argument in the torso of the for
loop is what's causing the mistake: assortment[i].carve up("");
. The length of array
is 9, so i
would quickly go way over the maximum length of array
:
function findLongestWord(str) { for(let i = 0; i < str.length; i++) { const array = str.divide(" "); console.log(array[i]); // array[0]: "The" // array[1]: "quick" // array[2]: "brown" // ... // array[9]: "dog" // assortment[ten]: undefined // array[11]: undefined } } findLongestWord("The quick brown play tricks jumped over the lazy dog");
Calling array[i].split("");
to split each cord into substrings of characters is a valid approach, only it will throw TypeError: Cannot read property 'divide' of undefined
when it's passed undefined
.
How to solve Find the Longest Word in a String with split
Allow's quickly become over some pseudo code for how to solve this problem:
- Separate
str
into an assortment of individual words - Create a variable to runway the greatest give-and-take length
- Iterate through the assortment of words and compare the length of each discussion to the variable mentioned above
- If the length of the current word is greater than the 1 stored in the variable, replace that value with the electric current word length
- Once the length of every give-and-take is compared with the maximum word length variable, return that number from the part
First, split str
into an array of private words:
function findLongestWordLength(str) { const array = str.split(" "); }
Create a variable to keep track of the longest word length and prepare information technology to nix:
function findLongestWordLength(str) { const assortment = str.split(" "); let maxWordLength = 0; }
Now that the value of array
is ['The', 'quick', 'brownish', 'fob', 'jumped', 'over', 'the', 'lazy', 'domestic dog']
, you can apply array.length
in your for
loop:
office findLongestWordLength(str) { const array = str.split up(" "); let maxWordLength = 0; for (permit i = 0; i < array.length; i++) { } }
Iterate through the assortment of words and bank check the length of each word. Think that strings also have a length
method you can call to easily get the length of a string:
function findLongestWordLength(str) { const array = str.divide(" "); let maxLength = 0; for (let i = 0; i < assortment.length; i++) { assortment[i].length; } }
Use an if
statement check if the length of the electric current give-and-take (array[i].length
) is greater than maxLength
. If and so, supplant the value of maxLength
with array[i].length
:
function findLongestWordLength(str) { const assortment = str.split(" "); allow maxLength = 0; for (let i = 0; i < array.length; i++) { if (array[i].length > maxLength) { maxLength = array[i].length; } } }
Finally, return maxLength
at the stop of the function, afterward the for
loop:
function findLongestWordLength(str) { const array = str.dissever(" "); let maxLength = 0; for (let i = 0; i < array.length; i++) { if (array[i].length > maxLength) { maxLength = array[i].length; } } return maxLength; }
Learn to code for free. freeCodeCamp'southward open source curriculum has helped more than 40,000 people become jobs as developers. Get started
weberentrught1943.blogspot.com
Source: https://www.freecodecamp.org/news/cannot-read-property-split-of-undefined-error/
0 Response to "Cannot Read Property of Undefined Mocha React"
Post a Comment