文章目录
- codewars-js练习
- 2021/1/18
- github 地址
- 【1】In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out.==(即删除数组中的字符串)==
- 【2】Complete the method that takes a boolean value and return a `"Yes"` string for `true`, or a `"No"` string for `false`.==(即遇到true返回Yes,遇到fasle返回No)==
- 【3】Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed.==(即返回数组中最小两个正整数的和)==
- 【4】Simple, given a string of words, return the length of the shortest word(s).==(即返回最短单词的长度)==
- 【5】Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.==(即删除相邻的重复元素)==
codewars-js练习
2021/1/18
github 地址
【1】In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out.(即删除数组中的字符串)
example:
filter_list([1,2,'a','b']) == [1,2]
filter_list([1,'a','b',0,15]) == [1,0,15]
filter_list([1,2,'aasf','1','123',123]) == [1,2,123]
思路:
- 创建新数组,用来放置原数组除去string类型的其他元素
- 判断原数组中不是string类型的元素
- 将其放入到新数组中
solution:
<script type="text/javascript">
function filter_list(l) {
// 创建新数组,放置原数组除去string类型的其他元素
var newL = [];
for(var i=0;i<l.length;i++){
// 判断是否为string类型,若不是,则插入到新数组中
if(typeof l[i]!='string'){
newL.push(l[i]);
}
}
// 返回新数组
return newL;
}
// 验证
var test1 = filter_list([1,2,'a','b']);
var test2 = filter_list([1,'a','b',0,15]);
var test3 = filter_list([1,2,'aasf','1','123',123]);
console.log(test1);//[1,2]
console.log(test2);//[1,0,15]
console.log(test3);//[1,2,123]
</script>
【2】Complete the method that takes a boolean value and return a "Yes"
string for true
, or a "No"
string for false
.(即遇到true返回Yes,遇到fasle返回No)
example:
boolToWord(true), 'Yes'
boolToWord(false), 'No'
solution:
<script type="text/javascript">
function boolToWord( bool ){
if(bool == true){
return 'Yes';
}else{
return 'No';
}
}
//验证
console.log(boolToWord(false));
</script>
【3】Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed.(即返回数组中最小两个正整数的和)
example:
[19, 5, 42, 2, 77]//7
[10, 343445353, 3453445, 3453545353453]//3453455
思路:
- 对数组进行升序排序,找到最小的两个元素
- 求和,返回
solution:
<script type="text/javascript">
function sumTwoSmallestNumbers(numbers) {
// 对数组进行升序排序
numbers = numbers.sort(function (x,y) {
return x-y;
});
// 对最小的两个元素求和
var sum = numbers[0]+numbers[1];
return sum;
}
// 验证
console.log(sumTwoSmallestNumbers([5, 8, 12, 19, 22]));//13
console.log(sumTwoSmallestNumbers([15, 28, 4, 2, 43]));//6
</script>
【4】Simple, given a string of words, return the length of the shortest word(s).(即返回最短单词的长度)
example:
findShort("bitcoin take over the world maybe who knows perhaps")// 3
findShort("turns out random test cases are easier than writing out basic ones")//3
思路:
- 先将字符串按空格分割;
- 遍历获取每个单词的长度,并放入数组中
- 对数组进行升序排序,返回最小长度
solution:
<script type="text/javascript">
function findShort(s){
// 将字符串按空格切割放入到数组中
var arr = s.split(' ');
// 创建lengthArr数组,放置每个单词的长度
var lengthArr = [];
// 创建minLength,放置最短单词长度
var minLength = 0;
// 遍历字符串分割后数组,并将每个单词长度放到新数组中
for(var i=0;i<arr.length;i++){
lengthArr.push(arr[i].length);
}
// 对单词长度进行升序排序
lengthArr = lengthArr.sort(function(x,y){
return x-y;
});
// 获取最短单词长度
minLength = lengthArr[0];
return minLength;
}
// 验证
console.log(findShort("bitcoin take over the world maybe who knows perhaps"));//3
console.log(findShort("turns out random test cases are easier than writing out basic ones"));//3
</script>
【5】Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.(即删除相邻的重复元素)
example:
uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder([1,2,2,3,3]) == [1,2,3]
思路:
- 先判断是string类型还是数组,若为string类型则将其分割为数组进行操作;
- 创建临时变量和结果返回数组;
- 遍历原数组,并判断是否相邻又重复;
- 若当前元素与临时变量不等,则加入到返回数组中,并更新临时变量;
- 返回最终返回数组。
solution:
<script type="text/javascript">
var uniqueInOrder=function(iterable){
// 先判断是string类型还是数组,若为string类型则将其分割为数组进行操作
if(typeof iterable == 'string'){
var arr = iterable.split('');
}else{
var arr = iterable;
}
// 创建临时变量
var pre_item = 0;
var resultArr = [];
// 若当前元素与临时变量不等,则加入到返回数组中,并更新临时变量
for(var i=0;i<arr.length;i++){
if(arr[i] != pre_item){
resultArr.push(arr[i]);
pre_item = arr[i];
}
}
return resultArr;
}
// 验证
uniqueInOrder('ABBCcAD');//['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder('AAAABBBCCDAABBB');//['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder([1,2,2,3,3]) //[1,2,3]
uniqueInOrder([]);//[]
</script>
以上为自己思路供大家参考,可能有更优的思路。