codewars练习记录15 js

[7 kyu] Convert an array of strings to array of numbers

Some really funny web dev gave you a sequence of numbers from his API response as an sequence of strings!

You need to cast the whole array to the correct type.

Create the function that takes as a parameter a sequence of numbers represented as strings and outputs a sequence of numbers.

ie:[“1”, “2”, “3”] to [1, 2, 3]

Note that you can receive floats as well.
翻译:
哦,不!
一些非常有趣的web开发人员从他的API响应中给出了一系列数字作为字符串序列!
您需要将整个数组转换为正确的类型。
创建一个函数,该函数将一系列表示为字符串的数字作为参数,并输出一系列数字。
即:[“1”、“2”、“3”]至[1、2、3]
请注意,您也可以接收浮动。
解:

function toNumberArray(stringarray)
{
  return stringarray.map(Number);
}
[8 kyu] Regex count lowercase letters

Your task is simply to count the total number of lowercase letters in a string.

Examples

lowercaseCount(“abc”); ===> 3
lowercaseCount(“abcABC123”); ===> 3
lowercaseCount(“abcABC123!@€£#KaTeX parse error: Expected 'EOF', got '#' at position 92: …unt("ABC123!@€£#̲%^&*()_-+=}{[]|':;?/>.<,~”); ===> 0
lowercaseCount(“abcdefghijklmnopqrstuvwxyz”); ===> 26

翻译:
您的任务只是计算字符串中小写字母的总数。
解一:

function lowercaseCount(str){
    return (str.match(/[a-z]/g) || []).length
}

解二:

function lowercaseCount(str){
    let count = 0
  for (let i = 0; i < str.length; i++) {
    if (str[i].charCodeAt() > 96 && str[i].charCodeAt() < 123) { count++ }
  }
  return count
}
[8 kyu] Exclamation marks series #6: Remove n exclamation marks in the sentence from left to right

Remove n exclamation marks in the sentence from left to right. n is positive integer.

Examples

remove(“Hi!”,1) === “Hi”
remove(“Hi!”,100) === “Hi”
remove(“Hi!!!”,1) === “Hi!!”
remove(“Hi!!!”,100) === “Hi”
remove(“!Hi”,1) === “Hi”
remove(“!Hi!”,1) === “Hi!”
remove(“!Hi!”,100) === “Hi”
remove(“!!!Hi !!hi!!! !hi”,1) === “!!Hi !!hi!!! !hi”
remove(“!!!Hi !!hi!!! !hi”,3) === “Hi !!hi!!! !hi”
remove(“!!!Hi !!hi!!! !hi”,5) === “Hi hi!!! !hi”
remove(“!!!Hi !!hi!!! !hi”,100) === “Hi hi hi”

翻译:
从左到右去掉句子中的n个感叹号。n是正整数。
解:

function remove(s,n){
  for (var i=0;i<n;i++) s=s.replace("!","");
  return s;
}
[8 kyu] Kata Example Twist

This is an easy twist to the example kata (provided by Codewars when learning how to create your own kata).

Add the value “codewars” to the array websites/Websites 1,000 times.
翻译:
这是对示例kata(由Codewars在学习如何创建自己的kata时提供)的简单修改。
将值“codewars”添加到阵列网站/网站1000次。
解:

var websites = new Array(1000).fill("codewars");
[7 kyu] Sort the Gift Code

Happy Holidays fellow Code Warriors!
Santa’s senior gift organizer Elf developed a way to represent up to 26 gifts by assigning a unique alphabetical character to each gift. After each gift was assigned a character, the gift organizer Elf then joined the characters to form the gift ordering code.

Santa asked his organizer to order the characters in alphabetical order, but the Elf fell asleep from consuming too much hot chocolate and candy canes! Can you help him out?

Sort the Gift Code
Write a function called sortGiftCode/sort_gift_code/SortGiftCode that accepts a string containing up to 26 unique alphabetical characters, and returns a string containing the same characters in alphabetical order.

Examples (Input – => Output):

“abcdef” – => “abcdef”
“pqksuvy” – => “kpqsuvy”
“zyxwvutsrqponmlkjihgfedcba” – => “abcdefghijklmnopqrstuvwxyz”

翻译:
代码战士们,节日快乐!
圣诞老人的高级礼物组织者Elf开发了一种方法,通过为每个礼物指定一个独特的字母字符来表示多达26份礼物。在给每个礼物分配了一个角色后,礼物组织者精灵将这些角色连接起来,形成礼物订购代码。
圣诞老人要求组织者按字母顺序排列角色,但小精灵因为吃了太多热巧克力和糖果而睡着了!你能帮他吗?
对礼品代码进行排序
编写一个名为sortGiftCode/sort_gift_code/sortGiftCode的函数,该函数接受最多包含26个唯一字母字符的字符串,并返回按字母顺序包含相同字符的字符串。
解:

function sortGiftCode(code){
  return code.split('').sort().join('');
}
[7 kyu] Gauß needs help! (Sums of a lot of numbers).

Due to another of his misbehaved, the primary school’s teacher of the young Gauß, Herr J.G. Büttner, to keep the bored and unruly young schoolboy Karl Friedrich Gauss busy for a good long time, while he teaching arithmetic to his mates, assigned him the problem of adding up all the whole numbers from 1 through a given number n.

Your task is to help the young Carl Friedrich to solve this problem as quickly as you can; so, he can astonish his teacher and rescue his recreation interval.

Here’s, an example:

f(n=100) // returns 5050

It’s your duty to verify that n is a valid positive integer number. If not, please, return false (None for Python, null for C#, 0 for COBOL).

Note: the goal of this kata is to invite you to think about some ‘basic’ mathematic formula and how you can do performance optimization on your code.

Advanced - experienced users should try to solve it in one line, without loops, or optimizing the code as much as they can.
翻译:
由于另一个行为不端的原因,年轻的Gauß的小学老师J.G.Büttner先生,为了让这个无聊而不守规矩的年轻男孩Karl Friedrich Gauss在给他的同学们教算术的同时,让他把从1到给定数字n的所有整数加起来。
你的任务是帮助年轻的卡尔·弗里德里希尽快解决这个问题;这样,他就可以让老师大吃一惊,挽救他的娱乐时间。
下面是一个例子:

f(n=100)//返回5050

你有责任验证n是一个有效的正整数。如果不是,请返回false(Python为无,C#为空,COBOL为0)。
注意:这个kata的目的是邀请你思考一些“基本”的数学公式,以及如何对代码进行性能优化。
高级经验丰富的用户应该尝试用一行代码来解决这个问题,而不需要循环,或者尽可能地优化代码
解:

function f(n){
return /(^[1-9]\d*$)/.test(n) ? (1+n)*(n/2) : false
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值