codewars练习记录18 js

本文提供了一系列简单但实用的编程挑战题目,涵盖了寻找最近的平方数、判断复数形式、用户名验证等,适合初学者实践和提高。

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

[8 kyu] Find Nearest square number

Your task is to find the nearest square number, nearest_sq(n), of a positive integer n.
翻译:
您的任务是找到正整数n的最近平方数nearest_sq(n)。
解:

function nearestSq(n){
    return Math.pow(Math.round(Math.sqrt(n)), 2);
}
[8 kyu] Plural

We need a simple function that determines if a plural is needed or not. It should take a number, and return true if a plural should be used with that number or false if not. This would be useful when printing out a string such as 5 minutes, 14 apples, or 1 sun.

You only need to worry about english grammar rules for this kata, where anything that isn’t singular (one of something), it is plural (not one of something).

All values will be positive integers or floats, or zero.
翻译:
我们需要一个简单的函数来确定是否需要复数。它应该接受一个数字,如果复数应该与该数字一起使用,则返回true,如果不应该,则返回false。当打印出5分钟、14个苹果或1个太阳等字符串时,这将非常有用。
你只需要担心这个kata的英语语法规则,任何不是单数的东西(某个事物之一)都是复数的(不是某个事物的一个)。
所有值都将是正整数或浮点数或零。
解:

function plural(n) {
  return n !== 1;
}
[8 kyu] Simple validation of a username with regex

Write a simple regex to validate a username. Allowed characters are:
lowercase letters,
numbers,
underscore
Length should be between 4 and 16 characters (both included).
解:

function validateUsr(username) {
  return /^[0-9a-z_]{4,16}$/.test(username)
}
[7 kyu] max diff - easy

You must implement a function that returns the difference between the largest and the smallest value in a given list / array (lst) received as the parameter.

lst contains integers, that means it may contain some negative numbers
if lst is empty or contains a single element, return 0
lst is not sorted

[1, 2, 3, 4] // returns 3 because 4 - 1 == 3
[1, 2, 3, -4] // returns 7 because 3 - (-4) == 7

翻译:
您必须实现一个函数,该函数返回作为参数接收的给定列表/数组(lst)中最大值和最小值之间的差值。
lst包含整数,这意味着它可能包含一些负数
如果lst为空或包含单个元素,则返回0
lst未排序
解:

function maxDiff(list) {
  return list.length ? Math.max(...list) - Math.min(...list) : 0;
};
[7 kyu] Build a square

I will give you an integer. Give me back a shape that is as long and wide as the integer. The integer will be a whole number between 1 and 50.

Example
n = 3, so I expect a 3x3 square back just like below as a string:

+++
+++
+++

翻译:
我给你一个整数。给我一个和整数一样长和宽的形状。整数将是介于1和50之间的整数。
解:

function generateShape(n){
  return ("+".repeat(n)+"\n").repeat(n).trim()
}

解二:

function generateShape(integer){
  let str = ''
  for (let i = 1; i <= integer; i++) {
    for (let j = 0; j < integer; j++) {
      str = str + '+'
    }
    str = str + '\n'
  }
   let arr = str.split('\n')
  arr.pop()
  return arr.join('\n')
}
[8 kyu] Template Strings

Template Strings
Template Strings, this kata is mainly aimed at the new JS ES6 Update introducing Template Strings
Task
Your task is to return the correct string using the Template String Feature.
Input

Two Strings, no validation is needed.

Output

You must output a string containing the two strings with the word ' are '

解:

var TempleStrings = function(obj, feature) {
  return `${obj} are ${feature}`;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值