文章目录
codewars-js练习
2021/4/14
github 地址
【1】<8kyu>【Do you speak “English”?】
solution
<script type="text/javascript">
function spEng(sentence){
console.log(sentence)
return (/english/i).test(sentence)
}
// 验证
console.log(spEng("english"));//true
console.log(spEng("egnlish"));//false
</script>
【2】<7kyu>【Filter Long Words】
返回一个包含所有长于n的单词的列表。
example:
filterLongWords("The quick brown fox jumps over the lazy dog", 4) = ['quick', 'brown', 'jumps']
solution
<script type="text/javascript">
function filterLongWords(sentence, n) {
console.log(sentence,n)
var arr = sentence.split(' ');
var result = [];
for(var i=0;i<arr.length;i++){
if(arr[i].length >n){
result.push(arr[i]);
}
}
return result
}
</script>
【3】<6kyu>【English beggars】
给定一组值和一个乞丐,你应该返回一个数组,每个乞丐带回家的总和,假设他们都进行有规律的转动,从第一个到最后一个。
example:
For example: [1,2,3,4,5] for 2 beggars will return a result of [9,6], as the first one takes [1,3,5], the second collects [2,4].
The same array with 3 beggars would have in turn have produced a better out come for the second beggar: [5,7,3], as they will respectively take [1,4], [2,5] and [3].
solution
<script type="text/javascript">
function beggars(values, n){
console.log(values,n)
var outputValues = [];
for (var i = 0; i < n; i++) {
var sum = 0;
for (var j = i; j < values.length; j += n) {
sum += values[j];
}
outputValues.push(sum);
}
return outputValues;
}
// 验证
console.log(beggars([1,2,3,4,5],1));//[15]
console.log(beggars([1,2,3,4,5],2));//[9,6]
console.log(beggars([1,2,3,4,5],3));//[5,7,3]
// console.log(beggars([1,2,3,4,5],6));//[1,2,3,4,5,0]
// console.log(beggars([1,2,3,4,5],0));//[]
</script>
【4】<8kyu>【Drink about】
- Children under 14 old.
- Teens under 18 old.
- Young under 21 old.
- Adults have 21 or more.
example:
13 --> "drink toddy"
17 --> "drink coke"
18 --> "drink beer"
20 --> "drink beer"
30 --> "drink whisky"
solution
<script type="text/javascript">
function peopleWithAgeDrink(old) {
console.log(old)
if(old<14)return 'drink toddy';
else if(old<18 && old>=14) return "drink coke";
else if(old<21 && old>=18) return "drink beer";
else return "drink whisky";
}
// 验证
console.log(peopleWithAgeDrink(22));//'drink whisky'
</script>
【5】<8kyu>【Keep up the hoop】
-If Alex gets 10 or more hoops, return the string “Great, now move on to tricks”.
-If he doesn’t get 10 hoops, return the string “Keep at it until you get it”.
solution
<script type="text/javascript">
function hoopCount (n) {
console.log(n)
if(n<10) return "Keep at it until you get it" ;
else return "Great, now move on to tricks"
}
// 验证
console.log(hoopCount(3));//"Keep at it until you get it"
console.log(hoopCount(11));//"Great, now move on to tricks"
</script>
【6】<8kyu>【Holiday VIII - Duty Free】
example:
12, 50, 1000// 166 --> 12 * 0.5=6 ---> 1000/6
solution
<script type="text/javascript">
function dutyFree(normPrice, discount, hol){
console.log(normPrice,discount,hol)
return Math.floor(hol / (normPrice * (discount/100)))
}
// 验证
console.log(dutyFree(12, 50, 1000));// 166
console.log(dutyFree(17, 10, 500));// 294
console.log(dutyFree(24, 35, 3000));// 357
</script>
本文记录了作者在2021年4月14日进行的JS Codewars练习,包括检查句子中是否包含'english'的正则表达式,过滤指定长度以上单词,按规律分配数值给乞丐,根据年龄推荐饮品,判断投篮次数达标情况以及计算免税商品数量。通过这些练习,展示了基础的JavaScript编程技巧和问题解决能力。
8万+

被折叠的 条评论
为什么被折叠?



