[7 kyu] Bumps in the Road
Your car is old, it breaks easily. The shock absorbers are gone and you think it can handle about 15 more bumps before it dies totally.
Unfortunately for you, your drive is very bumpy! Given a string showing either flat road () or bumps (n). If you are able to reach home safely by encountering 15 bumps or less, return Woohoo!, otherwise return Car Dead
翻譯:
描述:
你的車很舊,很容易壞。減震器不見了,你認(rèn)為它可以在完全失效之前再承受大約15次顛簸。
不幸的是,你的駕駛很顛簸!給定一個顯示平坦道路()或凹凸(n)的字符串。如果你能在15次或更少的顛簸中安全到家,請返回Woohoo!,否則返回Car Dead
解一:
function bump(x) {
let count = 0;
for (let i = 0; i < x.length; i++) {
if (x[i] == 'n') count++;
}
return count <= 15 ? 'Woohoo!' : 'Car Dead';
}
解二:
function bump(x) {
return x.split('n').length>16 ? "Car Dead" : "Woohoo!"
}
[7 kyu] Filling an array (part 1)
We want an array, but not just any old array, an array with contents!
Write a function that produces an array with the numbers 0 to N-1 in it.
For example, the following code will result in an array containing the numbers 0 to 4:
arr(5) // => [0,1,2,3,4]
Note: The parameter is optional. So you have to give it a default value.
翻譯:
我們想要一個數(shù)組,而不僅僅是任何舊數(shù)組,一個包含內(nèi)容的數(shù)組!
編寫一個函數(shù),生成一個包含數(shù)字0到N-1的數(shù)組。
注:該參數(shù)是可選的。所以你必須給它一個默認(rèn)值。
解一:
function arr(n){
var newArr = [];
for(var i = 0; i < n; i++){
newArr.push(i);
}
return newArr;
}
解二:
const arr = (n = 0) => [...Array(n).keys()]
[8 kyu] Regular Ball Super Ball
Create a class Ball. Ball objects should accept one argument for "ball type" when instantiated.
If no arguments are given, ball objects should instantiate with a "ball type" of "regular."
ball1 = new Ball();
ball2 = new Ball("super");
ball1.ballType //=> "regular"
ball2.ballType //=> "super"
翻譯:
創(chuàng)建一個類Ball。實例化時,Ball對象應(yīng)接受“Ball類型”的一個參數(shù)。
如果沒有給出參數(shù),ball對象應(yīng)該實例化為“常規(guī)”的“ball類型。
解:
var Ball = function(ballType) {
this.ballType = ballType || 'regular';
};
[8 kyu] altERnaTIng cAsE <=> ALTerNAtiNG CaSe
altERnaTIng cAsE <=> ALTerNAtiNG CaSe
Define String.prototype.toAlternatingCase (or a similar function/method such as to_alternating_case/toAlternatingCase/ToAlternatingCase in your selected language; see the initial solution for details) such that each lowercase letter becomes uppercase and each uppercase letter becomes lowercase. For example:
"hello world".toAlternatingCase() === "HELLO WORLD"
"HELLO WORLD".toAlternatingCase() === "hello world"
"hello WORLD".toAlternatingCase() === "HELLO world"
"HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld"
"12345".toAlternatingCase() === "12345" // Non-alphabetical characters are unaffected
"1a2b3c4d5e".toAlternatingCase() === "1A2B3C4D5E"
"String.prototype.toAlternatingCase".toAlternatingCase() === "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
As usual, your function/method should be pure, i.e. it should not mutate the original string.
大意就是:將大寫字母轉(zhuǎn)換成小寫,小寫字母轉(zhuǎn)換成大寫
解:
String.prototype.toAlternatingCase = function () {
return this.split("").map(a => a === a.toUpperCase()? a.toLowerCase(): a.toUpperCase()).join('')
}
[8 kyu] Basic Mathematical Operations
Your task is to create a function that does four basic mathematical operations.
The function should take three arguments - operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation.
Examples(Operator, value1, value2) --> output
('+', 4, 7) --> 11
('-', 15, 18) --> -3
('*', 5, 5) --> 25
('/', 49, 7) --> 7
翻譯:
您的任務(wù)是創(chuàng)建一個執(zhí)行四個基本數(shù)學(xué)運(yùn)算的函數(shù)。
該函數(shù)應(yīng)該有三個參數(shù)-operation(string/char)、value1(number)、value2(number)。
應(yīng)用所選操作后,函數(shù)應(yīng)返回數(shù)字結(jié)果。
解一:
function basicOp(operation, value1, value2) {
switch (operation) {
case '+':
return value1 + value2;
case '-':
return value1 - value2;
case '*':
return value1 * value2;
case '/':
return value1 / value2;
default:
return 0;
}
}
解二:
function basicOp(operation, value1, value2) {
return eval(value1+operation+value2);
}
[8 kyu] Double Char
Given a string, you have to return a string in which each character (case-sensitive) is repeated once.
Examples (Input -> Output):
"String" -> "SSttrriinngg"
"Hello World" -> "HHeelllloo WWoorrlldd"
"1234!_ " -> "11223344!!__ "
翻譯:
給定一個字符串,您必須返回其中每個字符(區(qū)分大小寫)重復(fù)一次的字符串。
解:
function doubleChar(str) {
return str.split('').map( x => x + x ).join('');
}