Skip to content Skip to sidebar Skip to footer

Com.google.gwt.core.client.javascriptexception: (Typeerror): Cannot Read Property 'b' of Null

Cannot Read Property 'split' of Undefined

If you've e'er used JavaScript's split method, there'due south a good chance that you've encountered the post-obit mistake: TypeError: Cannot read property 'split' of undefined.

There are a few reasons why you would receive this error. Most likely information technology's but a basic misunderstanding of how split works and how to iterate through arrays.

For example, if you try 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 assortment = str.separate(" ");     array[i].split("");   } }  findLongestWord("The quick brown fox jumped over the lazy dog");              

it volition throw the TypeError: Cannot read property 'dissever' of undefined mistake.

The split method

When separate is called on a cord, it splits the string into substrings based on the separator passed in as an argument. If an empty string is passed as an argument, split treats each character as a substring. Information technology and so returns an array containing the substrings:

                const testStr1 = "Examination test one 2"; const testStr2 = "cupcake pancake"; const testStr3 = "First,Second,Third";  testStr1.split(" "); // [ 'Examination', 'exam', '1', '2' ] testStr2.split(""); // [ 'c', 'u', 'p', 'c', 'a', 'g', 'east', ' ', 'p', 'a', 'n', 'c', 'a', 'one thousand', 'e' ] testStr3.divide(","); // [ 'Starting time', 'Second', 'Tertiary' ]                              

Check out MDN for more than details near split.

The problem explained with examples

Knowing what the split method returns and how many substrings yous tin can expect is the fundamental to solving this challenge.

Let'south take some other look at the code to a higher place and run across why information technology'south not working as expected:

                function findLongestWord(str) {    for(let i = 0; i < str.length; i++) {     const array = str.divide(" ");     assortment[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',   'brownish',   'fox',   'jumped',   'over',   'the',   'lazy',   'canis familiaris' ].

But take a closer wait at the for loop. Rather than using the length of array every bit a condition to iterate i, str.length is used instead.

str is "The quick brownish fox jumped over the lazy dog", and if yous log str.length to the console, you'll get 44.

The last statement in the trunk of the for loop is what'due south causing the fault: array[i].dissever("");. The length of array is ix, so i would quickly go fashion 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[nine]: "dog"     // array[10]: undefined     // assortment[xi]: undefined   } }  findLongestWord("The quick brown play tricks jumped over the lazy domestic dog");                              

Calling assortment[i].split(""); to split each string into substrings of characters is a valid approach, but it will throw TypeError: Cannot read holding 'split' of undefined when it's passed undefined.

How to solve Detect the Longest Give-and-take in a String with dissever

Let's rapidly get over some pseudo lawmaking for how to solve this problem:

  1. Split str into an array of individual words
  2. Create a variable to track the greatest word length
  3. Iterate through the assortment of words and compare the length of each discussion to the variable mentioned above
  4. If the length of the current word is greater than the one stored in the variable, supersede that value with the current word length
  5. Once the length of every discussion is compared with the maximum word length variable, render that number from the role

Start, split up str into an array of individual words:

                function findLongestWordLength(str) {   const array = str.split(" "); }              

Create a variable to keep rail of the longest word length and prepare it to zero:

                office findLongestWordLength(str) {   const assortment = str.split up(" ");   let maxWordLength = 0; }              

Now that the value of array is ['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'canis familiaris'], you can apply array.length in your for loop:

                office findLongestWordLength(str) {   const array = str.split up(" ");   permit maxWordLength = 0;      for (let i = 0; i < array.length; i++) {        } }              

Iterate through the array of words and bank check the length of each word. Think that strings as well have a length method y'all can phone call to easily get the length of a string:

                part findLongestWordLength(str) {   const array = str.split(" ");   allow maxLength = 0;      for (permit i = 0; i < assortment.length; i++) {     assortment[i].length;   } }              

Utilize an if statement bank check if the length of the current discussion (array[i].length) is greater than maxLength. If so, supersede 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 = assortment[i].length;     }   } }              

Finally, return maxLength at the terminate of the function, after the for loop:

                office findLongestWordLength(str) {   const array = str.dissever(" ");   let maxLength = 0;      for (let i = 0; i < assortment.length; i++) {     if (assortment[i].length > maxLength) {       maxLength = array[i].length;     }   }        return maxLength; }              

Learn to code for free. freeCodeCamp's open source curriculum has helped more than than twoscore,000 people go jobs as developers. Get started

norsworthyuntentoody.blogspot.com

Source: https://www.freecodecamp.org/news/cannot-read-property-split-of-undefined-error/

Postar um comentário for "Com.google.gwt.core.client.javascriptexception: (Typeerror): Cannot Read Property 'b' of Null"