7

I found a solution to this homework question, but I dont feel its the most efficient way to tackle the problem. Interested in other solutions I should explore.

Question: Write a function named allEqual that returns true if every character in the string is the same

Example:

If you pass "aaa" it should return true If you pass "aba" it should return false */

My Code

var stringAE = "aba";

function allEqual(string) {
    var stringAENew = "";
    for (var i = 0; i < string.length; i++) {
        if (string[0] === string[i]) {
            stringAENew += string[i];
            console.log(stringAENew)
        }

    }
    return stringAENew === string;
}


allEqual(stringAE) 
3
  • 3
    what's with the downvotes? Seems like a reasonable question me Commented Dec 16, 2016 at 21:54
  • 1
    I'm voting to close this question as off-topic because it is asking for a code review Commented Dec 16, 2016 at 22:19
  • 1
    Thanks @viaz - Im super appreciative of all the helpful folks on here and there is always usually one or two people every question or who act like they came out of the womb knowing how to code. its just more comical to read their comments. Commented Dec 16, 2016 at 22:44

6 Answers 6

12

Simple solution using .every().

function allEqual(input) {
    return input.split('').every(char => char === input[0]);
}

console.log(allEqual('aba')); // false
console.log(allEqual('aaa')); // true
console.log(allEqual('')); // true

Sign up to request clarification or add additional context in comments.

4 Comments

Using Array.every makes more sense here: var allEqual = str => str.split('').every( char => str[0] === char ) You don't have to go over the complete string if the chars do not match.
@DavidDomain You're right. I switched it to use some(). Same idea.
Sure, but using every already returns false whereas some returns true, so with every you would not have to negate the returned value. Anyway +1. ;)
Well, I guess I should rename the function to allDiff then ;). No, if the name is allEqual, every would indeed read better.
3

You can return false immediately once you find a character that doesn't match the first character. If you make it through the whole loop, return true because all the characters must have matched.

function allEqual(string) {
    for (var i = 1; i < string.length; i++) {
        if (string[i] != string[0]) {
            return false;
        }
    }
    return true;
}

You can also start your loop at i = 1, since the first character is obviously equal to itself, so there's no need to test it.

1 Comment

Unlike other solutions here, this one is the most efficient, since no need to loop on all characters, and for the first unique it breaks the loop.
2

Can be done with regex too

function allEqual(str) {
   return /^(.)\1*$/.test(str);
}

Although probably not so effective.

Comments

1

This ES6 solution also works for strings with Unicode code points in other than the first plane, i.e. with codes outside of the 16 bit range:

function allEqual(string) {
    return [...string].every( (x, _, a) => x === a[0]);
}

console.log(allEqual('aaaa')); // true
console.log(allEqual('aaaba')); // false
// Next one fails in solutions that don't support multi-plane unicode:
console.log(allEqual('𝌆𝌆𝌆')); // true
console.log(allEqual('')); // true

Comments

0

There's no reason to construct a result string. Just go over all the characters and compare them to the first one (as you've been doing). If you found a different character, the result is false. If you've gone over all the characters and haven't found a different one, the answer is true (note that this includes the edge cases of an empty string and a single character string):

function allEqual(string) {
    for (var i = 1; i < string.length; i++) {
        if (string[0] !== string[i]) {
            return false;
        }

    }
    return true;
}

Comments

0

I'm a little late for the party, but as I needed to do this on a project, I came up with another approach:

function allEqual(input) {
  return input === '' || new Set(input).size === 1;
}

console.log(['', 'aaa', '11', '####', 'aba', '12', '##@%', null, undefined].map(item => ({
  item,
  allEqual: allEqual(item),
})));

Comments

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.