文章目录
codewars-js练习
2021/2/1
github 地址
【1】<7kyu>【Anagram Detection】
An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).
Note: anagrams are case insensitive
Complete the function to return true
if the two arguments given are anagrams of each other; return false
otherwise.
字谜是将一个单词的字母重新排列以产生一个新单词的结果。注意:字谜不区分大小写。如果给定的两个参数是彼此的字谜,则完成函数返回true;否则返回false。
example:
"foefet" is an anagram of "toffee"
"Buckethead" is an anagram of "DeathCubeK"
solution
<script type="text/javascript">
var isAnagram = function(test, original) {
// 全部转为大写或小写方便比较,此处使用的全部小写
// 排序
var tStr = test.toLowerCase().split('').sort().join('');
var oStr = original.toLowerCase().split('').sort().join('');
console.log(tStr,oStr);
if(oStr===tStr){
return true;
}
return false;
};
// 验证
console.log(isAnagram("foefet", "toffee"));//true
console.log(isAnagram("Twoo", "WooT"));// true
console.log(isAnagram("dumble", "bumble"));//false
console.log(isAnagram("ound", "round"));// false
console.log(isAnagram("Buckethead", "DeathCubeK"));// true
console.log(isAnagram("apple", "pale"));// false
</script>
【2】<7kyu>【Round up to the next multiple of 5】
Given an integer as input, can you round it to the next (meaning, “higher”) multiple of 5?
四舍五入到下一个5的倍数
example:
input: output:
0 -> 0
2 -> 5
3 -> 5
12 -> 15
21 -> 25
30 -> 30
-2 -> 0
-5 -> -5
etc.
solution
<script type="text/javascript">
function roundToNext5(n){
console.log('n',n);
if(n %5 !=0){
return (Math.floor(n/5)+1)*5;
}
return n;
}
// 验证
[
[0,0],
[1,5],
[3,5],
[5,5],
[7,10],
[39,40],
[-2,0],
[-5,-5]
].forEach(
([x,out])=> console.log(roundToNext5(x), out)
);
</script>
【3】<7kyu>【Mumbling】
example:
accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"
solution
<script type="text/javascript">
function accum(s) {
// console.log(s);
var result = [s[0]];
for(var i=1;i<s.length;i++){
var ss = s[i].toUpperCase() + new Array(i+1).join(s[i].toLowerCase());
// console.log(ss);
result.push(ss);
}
return result.join('-');
}
// 验证
console.log(accum("ZpglnRxqenU"));// "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu"
</script>
知识点:重复n次
function repeatString(str,n) {
return new Array(n+1).join(str);
}
repeatString("a",3) // "aaa"
以上为自己思路供大家参考,可能有更优的思路。