【8 kyu】Reversing Words in a String
You need to write a function that reverses the words in a given string. A word can also fit an empty string. If this is not clear enough, here are some examples:
As the input may have trailing spaces, you will also need to ignore unneccesary whitespace.
Example (Input --> Output)
"Hello World" --> "World Hello"
"Hi There." --> "There. Hi"
翻译:
您需要编写一个函数来反转给定字符串中的单词。单词也可以容纳空字符串。如果这还不够清楚,下面是一些示例:
由于输入可能有尾随空格,您还需要忽略不必要的空白。
解:
function reverse(string){
return string.split(' ').reverse().join(' ')
}
【8 kyu】Merge two sorted arrays into one
You are given two sorted arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in asc order. Complete the function mergeArrays(arr1, arr2), where arr1 and arr2 are the original sorted arrays.
You don't need to worry about validation, since arr1 and arr2 must be arrays with 0 or more Integers. If both arr1 and arr2 are empty, then just return an empty array.
Note: arr1 and arr2 may be sorted in different orders. Also arr1 and arr2 may have same integers. Remove duplicated in the returned result.
Examples (input -> output)
*[1, 2, 3, 4, 5], [6, 7, 8, 9, 10] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*[1, 3, 5, 7, 9], [10, 8, 6, 4, 2] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*[1, 3, 5, 7, 9, 11, 12], [1, 2, 3, 4, 5, 10, 12] -> [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]
翻译:
给您两个排序数组,它们都只包含整数。您的任务是找到一种方法,将它们合并到一个单独的文件中,按asc顺序排序。完成函数mergeArrays(arr1,arr2),其中arr1和arr2是原始排序数组。
您不需要担心验证问题,因为arr1和arr2必须是包含0个或更多整数的数组。如果arr1和arr2都为空,则只返回一个空数组。
注:arr1和arr2可以按不同的顺序排序。arr1和arr2也可以有相同的整数。删除返回结果中的重复项。
解:
//数组拼接后去重再排序
function mergeArrays(arr1, arr2) {
arr1.push(...arr2)
return [...new Set(arr1)].sort((a,b)=>a-b)
}
【8 kyu】Add Length
What if we need the length of the words separated by a space to be added at the end of that same word and have it returned as an array?
Example(Input --> Output)
"apple ban" --> ["apple 5", "ban 3"]
"you will win" -->["you 3", "will 4", "win 3"]
Your task is to write a function that takes a String and returns an Array/list with the length of each word added to each element .
Note: String will have at least one element; words will always be separated by a space.
翻译:
如果我们需要在同一单词的末尾添加由空格分隔的单词的长度,并将其作为数组返回,该怎么办?
您的任务是编写一个函数,该函数接受一个String并返回一个Array/list,其中每个单词的长度添加到每个元素中。
注意:字符串将至少有一个元素;单词之间总是用空格隔开。
解:
function addLength(str) {
return str.split(" ").map(x=> x + " " +x.length)
}
【8 kyu】How old will I be in 2099?
Philip's just turned four and he wants to know how old he will be in various years in the future such as 2090 or 3044. His parents can't keep up calculating this so they've begged you to help them out by writing a programme that can answer Philip's endless questions.
Your task is to write a function that takes two parameters: the year of birth and the year to count years in relation to. As Philip is getting more curious every day he may soon want to know how many years it was until he would be born, so your function needs to work with both dates in the future and in the past.
Provide output in this format: For dates in the future: "You are ... year(s) old." For dates in the past: "You will be born in ... year(s)." If the year of birth equals the year requested return: "You were born this very year!"
"..." are to be replaced by the number, followed and proceeded by a single space. Mind that you need to account for both "year" and "years", depending on the result.
Good Luck!
翻译:
菲利普刚满四岁,他想知道未来几年他会多大,比如2090岁或3044岁。他的父母无法继续计算这个数字,所以他们恳求你帮他们写一个程序,可以回答菲利普没完没了的问题。
你的任务是编写一个函数,它需要两个参数:出生年份和与之相关的年份。由于菲利普每天都变得越来越好奇,他可能很快就会想知道他出生前还有多少年,所以你的函数需要处理未来和过去的两个日期。
以这种格式提供输出:对于未来的日期:“您是…岁。”对于过去的日期:”您将在…年出生。“如果出生年份等于要求的年份,请返回:”您是今年出生的!“
“…”应替换为数字,后跟一个空格。请注意,根据结果,您需要同时考虑“年”和“年”。
祝你好运!
解:
// 主要处理year单复数形式
function calculateAge(a,b) {
return a==b ? "You were born this very year!" : a>b ?
a-b==1 ? `You will be born in ${a-b} year.` : `You will be born in ${a-b} years.` :
b-a==1 ? `You are ${b-a} year old.` :`You are ${b-a} years old.`
}
【8 kyu】Find the position!
When provided with a letter, return its position in the alphabet.
Input :: "a"
Ouput :: "Position of alphabet: 1"
翻译:
如果提供了字母,请返回其在字母表中的位置
解:
function position(letter){
return `Position of alphabet: ${letter.toLowerCase().charCodeAt(0) - 96}`;
}
【7 kyu】Alternate capitalization
Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.
For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See test cases for more examples.
The input will be a lowercase string with no spaces.
翻译:
给定一个字符串,将分别占用偶数索引和奇数索引的字母大写,并按如下所示返回。索引0将被视为偶数。
解:
function capitalize(s){
let odd = s.split("").map((x, i) => i % 2 != 0 ? x.toUpperCase() : x).join("");
let even = s.split("").map((x, i) => i % 2 == 0 ? x.toUpperCase() : x).join("");
return [even, odd];
};
【7 kyu】Sorted? yes? no? how?
Complete the method which accepts an array of integers, and returns one of the following:
"yes, ascending" - if the numbers in the array are sorted in an ascending order
"yes, descending" - if the numbers in the array are sorted in a descending order
"no" - otherwise
You can assume the array will always be valid, and there will always be one correct answer.
翻译:
完成接受整数数组的方法,并返回以下值之一:
“yes, ascending”-如果数组中的数字按升序排序
“yes, descending”-如果数组中的数字按降序排序
“no”-否则
您可以假设数组总是有效的,并且总是有一个正确的答案。
解:
function isSortedAndHow(array) {
let arr = []
for (let i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
arr.push(true)
} else {
arr.push(false)
}
}
return arr.every(x => x == true) ? "yes, descending" : arr.every(x => x == false) ? "yes, ascending" : "no"
}
// 简便写法
/**
function isSortedAndHow(arr) {
return arr.every((x,i)=>i==0||arr[i]>=arr[i-1])?'yes, ascending':
arr.every((x,i)=>i==0||arr[i]<=arr[i-1])?'yes, descending':'no'
}
**/
【7 kyu】Factorial
Your task is to write function factorial
.
翻译:
您的任务是编写函数阶乘。
解一:
function factorial(n){
let sum = 1;
for (let i = 1; i <= n; i++){
sum = sum * i;
}
return sum;
}
解二:
function factorial(n){
return n ? n * factorial(n-1) : 1;
}
【8 kyu】Powers of 2
Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n ( inclusive ).
Examples
n = 0 ==> [1] ==> [2^0]
n = 1 ==> [1, 2] ==> [2^0, 2^1]
n = 2 ==> [1, 2, 4] ==> [2^0, 2^1, 2^2]
解:
function powersOfTwo(n){
var result = [];
for (var i = 0; i <= n; i++) {
result.push(Math.pow(2, i));
}
return result;
}