codewars练习记录22 js

本文解析了多个编程挑战题目,包括比赛结果输出、ASCII码转换、自守数判断等,通过具体示例展示了如何使用JavaScript来解决这些问题。

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

[8 kyu] UEFA EURO 2016

Finish the uefaEuro2016() function so it return string just like in the examples below:

uefaEuro2016([‘Germany’, ‘Ukraine’],[2, 0]) // “At match Germany - Ukraine, Germany won!”
uefaEuro2016([‘Belgium’, ‘Italy’],[0, 2]) // “At match Belgium - Italy, Italy won!”
uefaEuro2016([‘Portugal’, ‘Iceland’],[1, 1]) // “At match Portugal - Iceland, teams played draw.”

解:

function uefaEuro2016(teams, scores){
  if(scores[0] > scores[1]) return `At match ${teams[0]} - ${teams[1]}, ${teams[0]} won!`;
  if(scores[0] < scores[1]) return `At match ${teams[0]} - ${teams[1]}, ${teams[1]} won!`;
  if(scores[0] = scores[1]) return `At match ${teams[0]} - ${teams[1]}, teams played draw.`;
}
[7 kyu] Char Code Calculation

Given a string, turn each character into its ASCII character code and join them together to create a number - let’s call this number total1:

‘ABC’ --> ‘A’ = 65, ‘B’ = 66, ‘C’ = 67 --> 656667

Then replace any incidence of the number 7 with the number 1, and call this number ‘total2’:

total1 = 656667
total2 = 656661

Then return the difference between the sum of the digits in total1 and total2:

(6 + 5 + 6 + 6 + 6 + 7)
- (6 + 5 + 6 + 6 + 6 + 1)
---------------------------------6

解:

function calc(x){
  let sum = n => [...n].reduce((a,b) => +a + +b);
  let total1 = x.replace(/./g, x => x.charCodeAt(0));
  let total2 = total1.replace(/7/g,'1');
  return sum(total1) - sum(total2);
}
[7 kyu] Automorphic Number (Special Numbers Series #6)

Definition
A number is called Automorphic number if and only if its square ends in the same digits as the number itself.
Task
Given a number determine if it Automorphic or not .
翻译:
一个数被称为自守数,当且仅当它的平方以与数本身相同的数字结尾。
任务
给定一个数字,确定它是否自守。
解:

function automorphic(n){
  return String(n*n).endsWith(String(n)) ? "Automorphic" : "Not!!"
}
[8 kyu] The ‘if’ function

Create a function called _if which takes 3 arguments: a boolean value bool and 2 functions (which do not take any parameters): func1 and func2

When bool is truth-ish, func1 should be called, otherwise call the func2.

Example:

_if ( true ,
function() { console.log (“True”) },
function() { console.log (“false”) }
)
// Logs ‘True’ to the console.

翻译:
创建一个名为_if的函数,该函数接受3个参数:布尔值bool和2个函数(不接受任何参数):func1和func2
当bool为true-ish时,应该调用func1,否则调用func2。
解:

function _if(bool, func1, func2) {
  return bool ? func1() : func2();
}
[7 kyu] Round up to the next multiple of 5

Given an integer as input, can you round it to the next (meaning, “greater than or equal”) multiple of 5?
Examples:

input: output:
0 -> 0
2 -> 5
3 -> 5
12 -> 15
21 -> 25
30 -> 30
-2 -> 0
-5 -> -5
etc.

翻译:
四舍五入到下一个5的倍数
给定一个整数作为输入,你能把它四舍五入到下一个(意思是“大于或等于”)5的倍数吗?
解:

function roundToNext5(n){
  return Math.ceil(n/5)*5;
}
[8 kyu] Exclamation marks series #1: Remove an exclamation mark from the end of string

Remove an exclamation mark from the end of a string. For a beginner kata, you can assume that the input data is always a string, no need to verify it.

Examples

remove(“Hi!”) == “Hi”
remove(“Hi!!!”) == “Hi!!”
remove(“!Hi”) == “!Hi”
remove(“!Hi!”) == “!Hi”
remove(“Hi! Hi!”) == “Hi! Hi”
remove(“Hi”) == “Hi”

翻译:
删除字符串末尾的感叹号。对于初学者kata,您可以假设输入数据始终是字符串,无需验证。
解:

function remove(s) {
  return s.endsWith('!') ? s.slice(0, -1) : s;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值