最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Function that checks whether all characters in a string are equal javascript - Homework Warning - Stack Overflow

programmeradmin1浏览0评论

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) 

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) 
Share Improve this question asked Dec 16, 2016 at 21:44 EKREKR 1871 silver badge10 bronze badges 3
  • 3 what's with the downvotes? Seems like a reasonable question me – VLAZ 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 – Quentin 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. – EKR Commented Dec 16, 2016 at 22:44
Add a comment  | 

6 Answers 6

Reset to default 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

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.

Can be done with regex too

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

Although probably not so effective.

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('
发布评论

评论列表(0)

  1. 暂无评论