文章目录
codewars-js练习
2021/1/28
github 地址
【1】<7kyu>【Simple Fun #176: Reverse Letter】
Given a string str
, reverse it omitting all non-alphabetic characters.
example:
For str = "krishan", the output should be "nahsirk".
For str = "ultr53o?n", the output should be "nortlu".
solution
<script type="text/javascript">
function reverseLetter(str) {
console.log(str);
// 先匹配出全为字母的部分
var strArr = str.match(/[a-zA-Z]/g);
// console.log(strArr);
var arr = [];
// 遍历将字母逆序
for(var i=0;i<strArr.length;i++){
arr.unshift(strArr[i]);
}
// console.log(arr);
// 拼接成字符串返回
result = arr.join('');
return result;
}
// 验证
console.log(reverseLetter("krishan"));//"nahsirk")
console.log(reverseLetter('ultr53o?n'));//nortlu
</script>
【2】<7kyu>【Find the middle element】
As a part of this Kata, you need to create a function that when provided with a triplet, returns the index of the numerical element that lies between the other two elements.
The input to the function will be an array of three distinct numbers (Haskell: a tuple).
您需要创建一个函数,当提供三元组时,返回位于其他两个元素之间的数值元素的索引。函数的输入将是一个由三个不同数字组成的数组(Haskell:一个元组)。
example:
gimme([2, 3, 1]) // 0 -> 2 is the number that fits between 1 and 3 and the index of 2 in the input array is 0.
gimme([5, 10, 14]) // 1 -> 10 is the number that fits between 5 and 14 and the index of 10 in the input array is 1.
solution
<script type="text/javascript">
var gimme = function (inputArray) {
// console.log(inputArray);
// 先深拷贝原数组
var arr = JSON.parse(JSON.stringify(inputArray))
// var arr = inputArray;
// 先将数组排序找出第二个数(此处按照降序排序)->sort改变了原数组
inputArray.sort((a,b) =>{return b-a;});
// 经过排序后,arr为原数组,inputArray为排序后的数组
// console.log(arr);
// console.log(inputArray);
// 找出原数组中和该数相同的index
for(var i=0;i<arr.length;i++){
if(arr[i] == inputArray[1]){
return i;
}
}
};
// 验证
console.log(gimme([2, 3, 1]));// 0
console.log(gimme([5,10,14]));// 1
</script>
【3】<7kyu>【Vowel Count】
Return the number (count) of vowels in the given string.
We will consider a, e, i, o, u
as vowels for this Kata (but not y
).
The input string will only consist of lower case letters and/or spaces.
返回给定字符串中元音的数量(count)。输入只包含小写字母或空格
example:
getCount("abracadabra")// 5
solution
<script type="text/javascript">
function getCount(str) {
console.log(str)
var vowelsCount = 0;
// 用正则表达式匹配到含有元音字母部分
var result = str.match(/[aeiou]/g);
console.log(result);
if(result == null){
return 0;
}
vowelsCount = result.length;
return vowelsCount;
}
// 验证
console.log(getCount("abracadabra"));// 5
console.log(getCount('my pyx'))//0
</script>
【4】<7kyu>【Alternate capitalization】
Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0
will be considered even.
给定一个字符串,将占用偶数索引和奇数索引的字母分别大写,并返回如下所示。索引0将被认为是偶数。
example:
capitalize("abcdef")// ['AbCdEf', 'aBcDeF']
solution
<script type="text/javascript">
function capitalize(s){
var arr = s.split('');
// 深拷贝arr
var arr2 = JSON.parse(JSON.stringify(arr))
var len = arr.length;
var result =[];
for(var i=0;i<len;i++){
// 偶数索引--转为大写
if(i % 2 == 0){
// 将转为大写的字母替换掉原来字母
arr.splice(i,1,arr[i].toUpperCase());
}else{
// 对奇数索引处理
arr2.splice(i,1,arr[i].toUpperCase());
}
}
result.push(arr.join(''));
result.push(arr2.join(''));
return result;
}
// 验证
console.log(capitalize("abcdef"));// ['AbCdEf', 'aBcDeF']
console.log(capitalize("codewarriors"));//['CoDeWaRrIoRs', 'cOdEwArRiOrS']
</script>
【5】<7kyu>【Form The Minimum】
Given a list of digits, return the smallest number that could be formed from these digits, using the digits only once (ignore duplicates).
给定一个数字列表,返回可以由这些数字组成的最小数字,只使用这些数字一次(忽略重复)。
example:
minValue ([1, 3, 1]) // return (13)
minValue([5, 7, 5, 9, 7]) // return (579)
solution
<script type="text/javascript">
function minValue(values){
var temp = [];
for(var i=0;i<values.length;i++){
if(temp.indexOf(values[i])== -1){
temp.push(values[i]);
}
}
// console.log(temp);
// 不同数字的种类-->等同于最后返回数字所要包含几位数
var len = temp.length;
// 对其进行升序排序,然后连接成字符串,并转换成number即可
temp.sort((a,b)=>{return a-b;});
// console.log(temp);
var result = parseInt(temp.join(''));
return result;
}
// 验证
console.log(minValue([1,3,1]));//13
console.log(minValue([5,7,5,9,7]));//579
</script>
以上为自己思路供大家参考,可能有更优的思路。