codewars练习记录23 js

[8 kyu] Contamination #1 -String-

An AI has infected a text with a character!!
This text is now fully mutated to this character.
If the text or the character are empty, return an empty string.
There will never be a case when both are empty as nothing is going on!!
Note: The character is a string of length 1 or an empty string.
Example

text before = "abc"
character = "z"
text after = "zzz"

翻译:
一个AI感染了带有字符的文本!!
此文本现在已完全变异为此字符。
如果文本或字符为空,则返回空字符串。
永远不会有两个都空着的情况,因为什么都没有发生!!
注意:该字符是长度为1的字符串或空字符串。
解:

function contamination(text, char){
  return char.repeat(text.length)
}
[7 kyu] Product Of Maximums Of Array (Array Series #2)

Task
Given an array/list [] of integers , Find the product of the k maximal numbers.
Notes
Array/list size is at least 3 .
Array/list's numbers Will be mixture of positives , negatives and zeros
Repetition of numbers in the array/list could occur.
翻译:
给定整数数组/列表[],求k个最大数的乘积。
笔记
数组/列表大小至少为3。
数组/列表的数字将是正数、负数和零的混合
数组/列表中的数字可能会重复。
解一:

function maxProduct(numbers, size){
  let arr = numbers.sort((a, b) => b - a)
  let max = 1
  for (let i = 0; i < size; i++) {
    max = max * arr[i]
  }
  return max
}

解二:

function maxProduct(numbers, size){
  return numbers.sort((a, b) => b - a).slice(0, size).reduce((a, b) => a * b);
}
[8 kyu] Exclamation marks series #2: Remove all exclamation marks from the end of sentence

Remove all exclamation marks from the end of sentence.

Examples

remove("Hi!") === "Hi"
remove("Hi!!!") === "Hi"
remove("!Hi") === "!Hi"
remove("!Hi!") === "!Hi"
remove("Hi! Hi!") === "Hi! Hi"
remove("Hi") === "Hi"

翻译:
删除句末的所有感叹号。
解一:

function remove (string) {  
var i = string.length - 1;
while(string[i] == "!"){
   i--;
}
return string.substring(0,i+1);
  }

解二:

function remove(s){
  return s.replace(/!+$/, '');
}
[7 kyu] Currying functions: multiply all elements in an array

To complete this Kata you need to make a function multiplyAll/multiply_all which takes an array of integers as an argument. This function must return another function, which takes a single integer as an argument and returns a new array.
The returned array should consist of each of the elements from the first array multiplied by the integer.
Example:

multiplyAll([1, 2, 3])(2) = [2, 4, 6];

翻译:
要完成这个Kata,您需要创建一个函数multiplyAll/multiply_all,它以整数数组为参数。此函数必须返回另一个函数,该函数将单个整数作为参数并返回一个新数组。
返回的数组应该由第一个数组中的每个元素乘以整数组成。
解:

function multiplyAll(arr) {
  return function(n) {
    return arr.map(x => x * n);
  }
}
[8 kyu] Power

The goal is to create a function 'numberToPower(number, power)' that "raises" the number up to power (ie multiplies number by itself power times).
Examples

numberToPower(3,2) // -> 9 ( = 3 * 3 )
numberToPower(2,3) // -> 8 ( = 2 * 2 * 2 )
numberToPower(10,6) // -> 1000000

Note: Math.pow and some other Math functions like eval() and ** are disabled.
翻译:
目标是创建一个函数“numberToPower(number,power)”,将数字“提升”到幂(即将数字乘以自身的幂次)。
注:不能使用Math.pow和其他一些数学函数,如eval()和 **
解:

function numberToPower(number, power){
  var total = 1;
  for (var i = 1; i <= power; i++) { 
    total = total * number;
  }
  return total;
}
[7 kyu] Odd-Even String Sort

Given a string s. You have to return another string such that even-indexed and odd-indexed characters of s are grouped and groups are space-separated (see sample below)
Note:

0 is considered to be an even index.
All input strings are valid with no spaces
input: 'CodeWars'
output 'CdWr oeas'

S[0] = 'C'
S[1] = 'o'
S[2] = 'd'
S[3] = 'e'
S[4] = 'W'
S[5] = 'a'
S[6] = 'r'
S[7] = 's'

Even indices 0, 2, 4, 6, so we have 'CdWr' as the first group
odd ones are 1, 3, 5, 7, so the second group is 'oeas'
And the final string to return is 'Cdwr oeas'
翻译:
给定一个字符串s,您必须返回另一个字符串,以便对s的偶数索引字符和奇数索引字符进行分组,并对组进行空格分隔。
解:

const sortMyString = s => {
    let even = s.split('').filter((v, i) => i % 2 == 0).join('')
    let odd = s.split('').filter((v, i) => i % 2 != 0).join('')
    return even + ' ' + odd
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容