2

Trying to write a javascript function for comparing only first character of 2 strings but can't make it work. Here is the code.

function compare(wordOne, wordTwo) {
    if (wordOne.substring(0) === wordTwo.substring(0))
    {
        return true;
    } else {
        return false;
    }
}
compare("house", "hell");
5
  • String letters -> What exactly are you trying to do? Commented Apr 27, 2018 at 12:30
  • What is your code trying to achieve? You want to compare only first chaaracter? Commented Apr 27, 2018 at 12:30
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… with .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) Commented Apr 27, 2018 at 12:33
  • Yes sorry i didn't explain it really nice...but yes i only need to compare first character. Commented Apr 27, 2018 at 12:34
  • You can also treat strings as arrays of characters. str = "hello" means that str[1] = e Commented Apr 27, 2018 at 12:35

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

It would help make the code more reusable if you passed an index into the function and then returned wordOne[index] === wordTwo[index]
4

To explain why your current code doesn't work, you need to pass a second parameter to .substring as the to value. String.substring(0) just returns the whole string after the 0th character, so the entire word. Fixed example;

function compare(wordOne, wordTwo) {
    if (wordOne.substring(0, 1) === wordTwo.substring(0, 1)) {
        return true;
    }
    else 
    {
        return false;
    }
}

compare("house", "hell");

You could also just use wordOne[0] === wordTwo[0]

Comments

2

substring returns the part of the string between the start and end indexes, or to the end of the string.

If you want to compare only first character, use charAt

function compare(wordOne, wordTwo) {
   return wordOne.charAt(0) === wordTwo.charAt(0);
}
compare("house", "hell");

Or you can pass the index as the parameter

function compare(wordOne, wordTwo, index) {
   return wordOne.charAt(index) === wordTwo.charAt(index);
}
compare("house", "hell", 0);

1 Comment

you have luck that javascript is nice for the developer so that your second example would work as intended :P (add extra arg for clarity)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.