简体   繁体   中英

How to get each element's length in an array

 <html> <head> <title>This is the 3rd html</title> <!--Wei Wu section A--> <h1>This program wraps your text</h1> <script type="text/javascript"> /*function arrayToStr(){ var word = [] }*/ function splitString(stringToSplit, separator) { arrayOfStrings = stringToSplit.split(separator); //注意这儿能不能用document.write(arrayOfStrings + "<br>"); /*'The original string is: "' + stringToSplit + '"'; 'The separator is: "' + separator + '"') 'The array has ' + arrayOfStrings.length + ' elements: ' + arrayOfStrings.join(' / '))*/ } function longestWord(){ longest = arrayOfStrings[0].length; for (i=1; i<arrayOfStrings.length; i++){ if(longest<arrayOfStrings[i].length){ longest = arrayOfStrings[i].length; } else{ longest=longest; } } document.write(longest + "<br>"); } function splitUp(){ //arrayOfStrings = splitString(word," "); howLong = arrayOfStrings[0].length; for(i=0; i< arrayOfStrings.length;){ if (howLong <longest){ document.write(arrayOfStrings[i] + ' '); howLong = howLong + 1 + arrayOfStrings[i+1].length; i++; } else if (howLong = longest){ document.write(arrayOfStrings[i] + "<br>"); howLong = arrayOfStrings[i+1].length; i++; } else{ document.write("<br>" + arrayOfStrings[i] + " "); howLong = arrayOfStrings[i].length + 1 + arrayOfStrings[i+1].length; i++; } } } </script> </head> <body> <script type="text/javascript"> word = []; word = prompt("type a very long sentence that you want me to deal with: "); splitString(word, " "); longestWord(); splitUp(); </script> </body> </html> 

Hi all,

Thank you for your help. This assignment is to wrap text: 1. Includes an HTML input field for users to enter a string directly into a web page with an HTML button to execute the JavaScript. (I'll just skill the button part and to that later) 2. Includes a function that breaks the input string into individual words. 3. Includes a function to determine the longest single word in the input string (may be grouped with the previous function). 4. Includes a function to write the words of the string directly out to a web page, laid out so that no single line is longer than the longest word in the string.

I think for the first three, mine works well, but for the function that I wrote for the 4th part, it didn't work... I can not cut work into all those segment that a shorter than the longest word..

When I run your code with some input I get the following error:

Cannot read property 'length' of undefined

There is a point in your code where you're trying to access the length property of an object (string/array) that doesn't exist.

You should try running the code with a debugger/breakpoints/logging to see which of the calls to <string-or-array>.length is failing. Consider logging the input/output of each stage to see if it matches your expectations.

As an aside, defining word boundaries with a separator is problematic, although this may not be important for your assignment. If you are interested, take a look at regular expressions for an alternative to finding word boundaries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM