文章目录
codewars-js练习
2021/1/26
github 地址
【1】<8kyu>【Dollars and Cents】
you have volunteered to create a function that will take a float and return the amount formatting in dollars and cents.
39.99 becomes $39.99
example:
3 needs to become $3.00
3.1 needs to become $3.10
solution
<script type="text/javascript">
function formatMoney(amount){
var count = String(amount).length - (String(amount).indexOf(".") + 1);
if(amount % 1 ==0){
return '$'+amount +'.00';
}else if(count == 1){
return '$'+amount +'0';
}
return '$'+amount;
}
// 验证
console.log(formatMoney(39.99));// '$39.99'
</script>
【2】<6kyu>【Mexican Wave】
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.
-
The input string will always be lower case but maybe empty.
-
If the character in the string is whitespace then pass over it as if it was an empty seat
example:
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
solution
<script type="text/javascript">
function wave(str){
var arr = str.toLowerCase().split('');
// console.log(arr);
var result = [];
for(var i=0;i<arr.length;i++){
var temp = str.substring(0,i) + arr[i].toUpperCase() + str.substring(i+1);
if(temp != str){
result.push(temp);
}
}
// console.log(result);
return result;
}
// 验证
console.log(wave("hello"));// "Hello", "hEllo", "heLlo", "helLo", "hellO"
console.log(wave("gap"));// "Gap", "gAp", "gaP"
console.log(wave("two words"));//"Two words", "tWo words", "twO words", "two Words", "two wOrds", "two woRds", "two worDs", "two wordS";
</script>
【3】<6kyu>【Break camelCase】
Complete the solution so that the function will break up camel casing, using a space between words.
example:
solution("camelCasing") == "camel Casing"
solution
<script type="text/javascript">
function solution(string) {
// string字符串分隔而得到的数组
var arr = string.split('');
// 全部大写之后得到的数组
var strArr = string.toUpperCase().split('');
var temp = [];
// 获取到大写字母处的index
for(var i=0;i<arr.length;i++){
if(arr[i] == strArr[i]){
temp.push(i);
// console.log(temp);
}
}
// 找到位置,插入空格
var count = 0;
for(var j=0;j<temp.length;j++){
// 需要插入空格位置的index坐标
// console.log(temp[j]+count);
// splice删除元素,并添加新元素。
// splice(index,删除数量,新元素)
arr.splice(temp[j]+count,0,' ');
count ++;
}
return arr.join('');
}
// 验证
console.log(solution("camelCasing"));//"camel Casing"
console.log(solution('camelCasingTest'));// 'camel Casing Test', 'Unexpected result'
</script>
【4】<7kyu>【Factorial】
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1.
Write a function to calculate factorial for a given input. If input is below 0 or above 12 throw a RangeError
(JavaScript) .
求阶乘
example:
factorial(0)// 1
factorial(1)//1
factorial(2)// 2
factorial(3)// 6
solution
<script type="text/javascript">
function factorial(n){
// console.log('n',n);
var i =1;
var sum =1;
if(n>1 && n<=12){
while (i <= n) {
sum *= i;
i++;
}
return sum;
}else if(n == 0 || n == 1){
return 1;
}else{throw new RangeError();}
}
// 验证
console.log(factorial(0));//1
console.log(factorial(1));//1
console.log(factorial(5));//120
console.log(factorial(13));//报错
</script>
【5】<7kyu>【Categorize New Member】
To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.
Input will consist of a list of lists containing two items each. Each list contains information for a single potential member. Information consists of an integer for the person’s age and an integer for the person’s handicap.
Note for F#: The input will be of (int list list) which is a List
[age,handicap],只有当age>=55 并且handicap>7时才为senior,否则为open
example:
[[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]//["Open", "Open", "Senior", "Open", "Open", "Senior"]
solution
<script type="text/javascript">
function openOrSenior(data){
var str = '';
var result = [];
for(var i=0;i<data.length;i++){
//判断是否符合senior条件
if(data[i][0]>=55 && data[i][1]>7){
str = 'Senior';
result.push(str);
}else{
str = 'Open';
result.push(str);
}
}
// console.log(result);
return result;
}
// 验证
console.log(openOrSenior([[45, 12],[55,21],[19, -2],[104, 20]]));//['Open', 'Senior', 'Open', 'Senior'])
console.log(openOrSenior([[3, 12],[55,1],[91, -2],[54, 23]]));//['Open', 'Open', 'Open', 'Open']
console.log(openOrSenior([[59, 12],[55,-1],[12, -2],[12, 12]]));//['Senior', 'Open', 'Open', 'Open'])
</script>
以上为自己思路供大家参考,可能有更优的思路。