I want to compare user input to a given string eg 'hello' compared 'hello' should return true that part is easy but I also want 'h', 'he', 'hel' etc to return true but not 'lo'
How would you approach this with javascript?
A quick and simple way:
var match = "hello"; var test = "hel"; if( match.substr(0,test.length) == test) { // looking good! // optionally, add this to the condition: && test.length > 0 // otherwise an empty test string would match } match.substring(0, test.length) === test.you need to use indexOf() function.
var hello = "hello"; if(hello.indexOf("he")===0){ //its in. } === 0, not > 0.