Assuming you want to compare the first letter of the two strings, you can use the following code
function compare(wordOne, wordTwo) {
return wordOne[0] === wordTwo[0];
}
compare("house", "hell");
This condenses the if/else condition, as you are just interested in whether the first letters are equal - not in how different they are.
You can also use str.toUpperCase() (or) str.toLowerCase() in order to make the comparison case insensitive.
As per @Josh Katofsky's suggestion, you can of course make this function more versatile by - for instance - adding a third parameter that tests the n-th letter:
function compare(wordOne, wordTwo, index) {
return wordOne[index] === wordTwo[index];
}
compare("house", "hell", 0);
.substring()you have the option to include where the substring starts and ends (if end is omitted, it will capture to the end of the string)...wordOne.substring(0,1)str = "hello"means thatstr[1] = e