Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sushimus

[JavaScript] I can't understand a split error

@doertedev @vorticalbox I ended up just copying over his entire script and slotting in all the things I needed to replace for my project, I appreciate the tips though

Recommended Posts

Posted · Original PosterOP

So I know very little javascript, enough to kind of understand what's going on but that's about it. However I've finally found something I don't understand! I'm getting an error on a split and I've no idea why. For context I'm making a random phrase generator so I can use it for drawing ideas and I'm fairly closely basing it off someone else's, so I'm copy a fair bit of that guy's code. And I've copied all the code I thoguth I needed from it however one bit I copied is being difficult despite it being exactly the same as the working one.
Mine

 

function getTextBetweenTags(text, start_tag, end_tag) {
	return text.split(start_tag)[1].split(end_tag)[0];
}

His
 

function getTextBetweenTags(text, start_tag, end_tag) {
	return text.split(start_tag)[1].split(end_tag)[0];
}

From what I can gather from google's f12 functionality the .split(end_tag)[0]; is the issue, but I've also copied all the stuff containing end_tag AND their dependencies too. I know it's kinda taboo to link the actual files in the post but if someone can tell me why mine's being a butt I'd appreciate it. (Mine is the IdeaMachine)

Edit: the error google says is "Cannot read 'split' of undefined", and from what I can tell I think I've defined it?

 

Edit-re: I think I've figured it out?

IdeaMachine.js generator.js


-Undeniably Indie™

Link to post
Share on other sites

Easy beginner mistake. You're not checking enough. Any typesafe message would require you to state that text needs to be a string but in JS it can also be nil or undefined. So first I would check if text is actually a string. Then, when split, check if the split result is actually of length >= 2 (counted from zero, that gives you a valid first item). Then, also this result can be undefined or nil, so basically do the same as before again. When code looks equal but behaves different it's the input that makes out what works and what doesn't.

Link to post
Share on other sites

break up your code into steps so you can control when things go wrong

 

function getTextBetweenTags(text, start_tag, end_tag) {
    const sTag = text.split(start_tag)[1]
    if(!sTag){
        return null
    }
    return sTag.split(end_tag)[0];
}

though If were to solve this problem i would have done

 

const getBetweenTags = (text, startTag, endTag) =>{
  // plus one to remove the start tag
  const startIndex = text.indexOf(startTag) + 1;
  const endIndex = text.indexOf(endTag);
  return text.slice(startIndex, endIndex)
}


const result = getBetweenTags('hello #there?', '#', '?');
console.log(result) // there

only issue is that when the tag isn't found it defaults to -1 which will return funky things but you can check that the value of start and end are above -1 and you can check that the end is after the index

 


                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
Posted · Original PosterOP · Best Answer

@doertedev @vorticalbox I ended up just copying over his entire script and slotting in all the things I needed to replace for my project, I appreciate the tips though


-Undeniably Indie™

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×