codewars练习记录11 js

博客围绕JavaScript编程展开,包含多个编程题,如判断车能否在15次颠簸内到家、生成含特定数字的数组、创建Ball类、大小写字母转换、实现基本数学运算、字符重复等,并给出了部分题目的多种解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[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
翻译:
描述:
你的车很旧,很容易坏。减震器不见了,你认为它可以在完全失效之前再承受大约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.
翻译:
我们想要一个数组,而不仅仅是任何旧数组,一个包含内容的数组!
编写一个函数,生成一个包含数字0到N-1的数组。
注:该参数是可选的。所以你必须给它一个默认值。
解一:

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”

翻译:
创建一个类Ball。实例化时,Ball对象应接受“Ball类型”的一个参数。
如果没有给出参数,ball对象应该实例化为“常规”的“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.
大意就是:将大写字母转换成小写,小写字母转换成大写
解:

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

翻译:
您的任务是创建一个执行四个基本数学运算的函数。
该函数应该有三个参数-operation(string/char)、value1(number)、value2(number)。
应用所选操作后,函数应返回数字结果。
解一:

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!!__ "

翻译:
给定一个字符串,您必须返回其中每个字符(区分大小写)重复一次的字符串。
解:

function doubleChar(str) {
  return str.split('').map( x => x + x ).join('');
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值