0039【Edabit ★☆☆☆☆☆】【字符串长度比较】Compare Strings by Count of Characters
0039【Edabit ★☆☆☆☆☆】【比较字符串长度】Compare Strings by Count of Characters
conditions
strings
validation
Instructions
Create a function that takes two strings as arguments and return either
true
orfalse
depending on whether the total number of characters in the first string is equal to the total number of characters in the second string.
Examples
comp("AB", "CD") // true
comp("ABC", "DE") // false
comp("hello", "edabit") // false
Notes
- Don’t forget to
return
the result. - If you get stuck on a challenge, find help in the Resources tab.
- If you’re really stuck, unlock solutions in the Solutions tab.
Solutions
function comp(str1, str2) {
return str1.length == str2.length ;
}
TestCases
function comp(str1, str2) {
return str1.length == str2.length ;
}
let Test = (function(){
return {
assertEquals:function(actual,expected){
if(actual !== expected){
let errorMsg = `actual is ${actual},${expected} is expected`;
throw new Error(errorMsg);
}
},
assertSimilar:function(actual,expected){
if(actual.length != expected.length){
throw new Error(`length is not equals, ${actual},${expected}`);
}
for(let a of actual){
if(!expected.includes(a)){
throw new Error(`missing ${a}`);
}
}
}
}
})();
Test.assertEquals(comp("AB", "CD"), true)
Test.assertEquals(comp("ABC", "DE"), false)
Test.assertEquals(comp("hello", "edabit"), false)
Test.assertEquals(comp("meow", "woof"), true)
Test.assertEquals(comp("jrnvjrnnt", "cvjknfjvmfvnfjn"), false)
Test.assertEquals(comp("jkvnjrt", "krnf"), false)
Test.assertEquals(comp("Facebook", "Snapchat"), true)