文章目录
codewars-js练习
2021/1/23
github 地址
【1】<8kyu>【Find the position!】
When provided with a letter, return its position in the alphabet.
Input :: “a”
Ouput :: “Position of alphabet: 1”
example:
position("a"),"Position of alphabet: 1");
solution:
<script type="text/javascript">
function position(letter){
console.log(letter)
var result = letter.charCodeAt()-96;
return "Position of alphabet: " + result;
}
// 验证
console.log(position("a"));//"Position of alphabet: 1"
</script>
【2】<6kyu>【Count characters in your string】
The main idea is to count all the occurring characters in a string. If you have a string like aba
, then the result should be {'a': 2, 'b': 1}
.
What if the string is empty? Then the result should be empty object literal, {}
.
example:
count("aba")// { a: 2, b: 1 }
count("")//{}
solution:
<script type="text/javascript">
function count (string) {
// 创建空对象
var count = {
};
// 将string通过split转换成arr数组类型
var arr = string.split('');
// 遍历数组,并判断在count中是否存在s
arr.forEach(function(s){
count[s] ? count[s]++ : count[s] = 1;
})
return count;
}
// 验证
console.log(count("aba"));// { a: 2, b: 1 }
console.log(count(''));//{}
</script>
【3】<7kyu>【Testing 1-2-3】
Write a function which takes a list of strings and returns each line prepended by the correct number.
The numbering starts at 1. The format is n: string
. Notice the colon and space in between.
example:
number([]) // => []
number(["a", "b", "c"]) // => ["1: a", "2: b", "3: c"]
solution:
<script type="text/javascript">
var number=function(array){
var len = array.length;
if(len ==0){
return [];
}else{
for(var i=0;i<len;i++){
// 用"index+1: a" 代替 "a"
array.splice(i,1,parseInt(i+1)+ ": "+ array[i]);
}
return array;
}
}
// 验证
console.log(number([])); // => []
console.log(number(["a", "b", "c"])); // => ["1: a", "2: b", "3: c"]);
</script>
【4】<8kyu>【Sum of differences in array】
Your task is to sum the differences between consecutive pairs in the array in descending order.
example:
For example:
sumOfDifferences([2, 1, 10])
Returns 9
Descending order: [10, 2, 1]
Sum: (10 - 2) + (2 - 1) = 8 + 1 = 9
solution:
<script type="text/javascript">
function sumOfDifferences(arr) {
// 先将数组降序排序
arr = arr.sort(function(a,b){
return b-a;});
var arr1 = [];
var result = 0;
for(var i=0;i<arr.length-1;i++){
var sum = arr[i] - arr[i+1];
arr1.push(sum);
}
for(var j=0;j<arr1.length;j++){
var result = result + arr1[j];
}
return result;
}
// 验证
console.log(sumOfDifferences([-3, -2, -1])); // 2
console.log(sumOfDifferences([1, 2, 10])); // 9
</script>
【5】<6kyu>【Sort the odd】
You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.
即 你必须把奇数按升序排序,而把偶数保留在原来的位置。
example:
[7, 1] => [1, 7]
[5, 8, 6, 3, 4] => [3, 8, 6, 5, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] => [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
思路:
- 先将奇数和对应的index放在新数组
- 对奇数升序排序
- 将原数组中奇数部分替换成新的
solution:
<script type="text/javascript">
function sortArray(array) {
// 保存仅奇数的数组
var oddArr = [];
// 保存奇数的index值
var oddIndex = [];
// 找到奇数放入到相应的数组中
for(var i=0;i<array.length;i++){
if(array[i]%2!=0){
oddArr.push(array[i]);
oddIndex.push(i);
}
}
// 对奇数进行升序排序
oddArr.sort((a,b)=>{
return a-b;});
// 将原数组中奇数部分替换成新的
for(var j=0;j<oddArr.length;j++){
// console.log(array[oddIndex[j]]);
// console.log(oddArr[j])
array[oddIndex[j]] = oddArr[j];
}
// console.log(array);
return array;
}
// 验证
console.log(sortArray([5, 3, 2, 8, 1, 4])); // [1, 3, 2, 8, 5, 4]
</script>
以上为自己思路供大家参考,可能有更优的思路。