文章目录
codewars-js练习
2021/2/8
github 地址
【1】<7kyu>【Evens and Odds】
This kata is about converting numbers to their binary or hexadecimal representation:
- If a number is even, convert it to binary.
- If a number is odd, convert it to hex.
将数字转换成二进制或十六进制表示的:
如果一个数是偶数,就把它转换成二进制数。如果一个数字是奇数,将其转换为十六进制。
example:
(2));//'10'
(13));//'d'
solution
<script type="text/javascript">
function evensAndOdds(num){
// console.log(num);
if(num % 2 == 0){
return num.toString(2);
}else{
return parseInt(num).toString(16);
}
}
// 验证
console.log(evensAndOdds(2));//'10'
console.log(evensAndOdds(13));//'d'
</script>
【2】<7kyu>【Life Path Number】
一个人的生命路径号是通过将其出生日期中的每个数字相加计算出来的,直到它减少为一个数字。
example:
year: 1 + 8 + 7 + 9 = 25; 2 + 5 = 7
month: 0 + 3 = 3
day: 1 + 4 = 5
final result: 7 + 3 + 5 = 15; 1 + 5 = 6
solution
<script type="text/javascript">
function lifePathNumber(dateOfBirth) {
// console.log(dateOfBirth);
var arr = dateOfBirth.split('-').join('').split('');
// console.log(arr);
var year1 = parseInt(arr[0]) + +arr[1] + +arr[2] + +arr[3];
// console.log(year1)
var year = Math.floor(year1 / 10) + year1 % 10;
// console.log(year);
var mouth = parseInt(arr[4]) + +arr[5];
var day = parseInt(arr[6]) + +arr[7];
var final = year + mouth + day;
// console.log(final);
var lifenum = (Math.floor(final / 10) + final % 10);
if(lifenum % 10 == 0){
lifenum = (Math.floor(lifenum / 10) + lifenum % 10);
}
return lifenum;
}
// 验证
console.log(lifePathNumber("1879-03-14"));// 6
console.log(lifePathNumber("1815-12-10"));// 1
console.log(lifePathNumber('1961-07-04'));//1
</script>
【3】<7kyu>【Flatten and sort an array】
Given a two-dimensional array of integers, return the flattened version of the array with all the integers in the sorted (ascending) order.
给定一个二维整数数组,返回该数组的扁平版本,其中所有整数按排序(升序)顺序排列。
example:
Given [[3, 2, 1], [4, 6, 5], [], [9, 7, 8]], your function should return [1, 2, 3, 4, 5, 6, 7, 8, 9].
solution
<script type="text/javascript">
function flattenAndSort(array) {
// console.log(array);
var arr = [];
if(array.length !=0){
for(var i=0;i<array.length;i++){
for(var j=0;j<array[i].length;j++){
// console.log(array[i][j]);
arr.push(array[i][j]);
}
}
// console.log('arr',arr)
arr.sort((a,b)=>{return a-b;});
for(var z=0;z<arr.length;z++){
arr[z] = parseInt(arr[z]);
}
// console.log(arr);
return arr;
}else{
return array;
}
}
// // 验证
console.log(flattenAndSort([[3, 2, 1], [4, 6, 5], [], [9, 7, 8]]));//[1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(flattenAndSort([[1, 3, 5], [100], [2, 4, 6]]));// [1, 2, 3, 4, 5, 6, 100]
</script>
以上为自己思路供大家参考,可能有更优的思路。