I was attempting to split an element (which is an Array) of an Array, and then access that array's length.
A sample input could be: "He walks. She walks.".
My code (with unrelated parts removed) is:
function image(input, str) {
let nos = str.split(".");
for (let zz = 0; zz < (nos.length - 1); zz++) {
for (let z = 0; z < nos[zz].split(" ").length; z++) {
/*...*/
console.log(`zz: ${zz}, z: ${z}`);
}
}
}
image(null, "He walks. She walks.");
This gives an error:
Uncaught TypeError: Cannot read property 'length' of undefined
If your idea is just to running code from how much word that are included in (str) you can just try this
function image(input, str) {
let nos = str.split(" ");
for (let i = 0; i < nos.length; i++){
console.log("it will run 4x")
}
}
image(null, "He walks. She walks.")
This is if you want to keep your code, i remove the -1 at nos.length and add filter. This can be used for any str which is included (.) dot and (" ") space
function image(input, str) {
//first we remove (.) symbol
let nos = str.split(".");
for (let i = 0; i < (nos.length); i++){
// use filter(v => v!== "") to remove all empty space array
for (let x = 0; x < (nos[i].split(" ").filter(v => v !== "")).length; x++){
console.log("it will run 4 time")
}
}
}
image(null, "He walks. She walks.")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments