[英]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)
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.