简体   繁体   English

检查字符串中所有字符是否相等的函数javascript - 作业警告

[英]Function that checks whether all characters in a string are equal javascript - Homework Warning

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问题:编写一个名为 allEqual 的函数,如果字符串中的每个字符都相同,则返回 true

Example:例子:

If you pass "aaa" it should return true If you pass "aba" it should return false */如果你通过“aaa”它应该返回true 如果你通过“aba”它应该返回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) 

Simple solution using .every() .使用.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.一旦找到与第一个字符不匹配的字符,您可以立即返回false If you make it through the whole loop, return true because all the characters must have matched.如果您通过整个循环,则返回true因为所有字符都必须匹配。

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.您也可以从i = 1开始循环,因为第一个字符显然等于自身,因此无需对其进行测试。

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 , ie with codes outside of the 16 bit range:此 ES6 解决方案也适用于在第一个平面以外的 Unicode 代码点的字符串,即 16 位范围之外的代码:

 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

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 .如果您找到不同的字符,则结果为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):如果您已经浏览了所有字符并且没​​有找到不同的字符,那么答案是true (请注意,这包括空字符串和单个字符串的边缘情况):

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

    }
    return true;
}

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), })));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个JavaScript函数来检查字符串中的第一个和最后一个字符是否相等 - How to write a javascript function that checks if first and last characters in a string are equal 一个检测字符串的前半部分是否与后半部分匹配的函数-作业警告JS - A Function that Detects whether the first half of a string matches the second half - Homework Warning JS 函数接受一个字符串并返回一个字母和出现次数的对象-javascript - Homework Warning - Function takes a string and returns an object of the letters and number of occurrences -javascript - Homework Warning 检查数字是否为回文的 Javascript function - Javascript function that checks whether number is palindrome 检查字符串是否由所有唯一字符组成-不等于JavaScript中的运算符 - Check if a string is composed of all unique characters— not equal to operator in JavaScript 转义字符串等于JavaScript中的特殊字符 - Escape String equal special characters in JavaScript 检查是否全部在下的功能 - Function that checks if all in Lower 使用JavaScript检查字符串是否包含日文字符(包括汉字) - Using JavaScript to check whether a string contains Japanese characters (including kanji) 使用某种方法编写一个函数来检查数组中的一项是否等于 null (JavaScript) - Write a function using some method that checks if an item in the array is equal to null (JavaScript) 如何使用名称等于字符串的名称定义javascript函数 - How to define a javascript function with a name equal to a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM