【8 kyu】Multiplication table for number
Your goal is to return multiplication table for number that is always an integer from 1 to 10.
For example, a multiplication table (string) for number == 5 looks like below:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
P. S. You can use \n in string to jump to the next line.
Note: newlines should be added between rows, but there should be no trailing newline at the end. If you're unsure about the format, look at the sample tests.
翻译:
您的目标是返回始终为1到10之间的整数的乘法表。
解一:
function multiTable(number) {
var arr=[]
for(let i=1;i<=10;i++){
arr.push(i + ' * ' + number + ' = ' +i*number)
}
return arr.join('\n')
}
解二:
function multiTable(number ) {
return [...Array(10)].map((_,i)=>`${i+1} * ${number } = ${number *i+number }`).join('\n')
}
【7 kyu】Predict your age!
My grandfather always predicted how old people would get, and right before he passed away he revealed his secret!
In honor of my grandfather's memory we will write a function using his formula!
Take a list of ages when each of your great-grandparent died.
Multiply each number by itself.
Add them all together.
Take the square root of the result.
Divide by two.
翻译:
我祖父总是预测人们会多大年纪,就在他去世之前,他透露了他的秘密!
为了纪念我祖父,我们将用他的公式写一个函数!
列一张你的曾祖父母去世的年龄表。
将每个数字本身相乘。
把它们加在一起。
取结果的平方根。
除以二。
解一:
function predictAge(age1,age2,age3,age4,age5,age6,age7,age8){
let arr = [age1, age2, age3, age4, age5, age6, age7, age8]
return Math.floor(Math.sqrt(arr.map(x=> x* x).reduce((a,b) => a+ b)) / 2)
}
解二:
const predictAge = (...ages) => Math.hypot(...ages) / 2 | 0;
The Math.hypot() function returns the square root of the sum of squares of its arguments. 返回参数平方和的平方根
【7 kyu】Number of People in the Bus
There is a bus moving in the city, and it takes and drop some people in each bus stop.
You are provided with a list (or array) of integer pairs. Elements of each pair represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop.
Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D
Take a look on the test cases.
Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative.
The second value in the first integer array is 0, since the bus is empty in the first bus stop.
翻译:
城市里有一辆公共汽车在行驶,每一个公共汽车站都会载着一些人。
您将获得一个整数对列表(或数组)。每对元素代表在公共汽车站上车的人数(第一项)和下车的人数(第二项)。
您的任务是返回最后一个公共汽车站(在最后一个数组之后)后仍在公共汽车中的人数。尽管这是最后一个公共汽车站,但公共汽车并不是空的,有些人还在车里,他们可能睡在那里。
看看测试用例。
请记住,测试用例确保总线中的人数始终>=0。因此返回的整数不能为负数。
第一个整数数组中的第二个值为0,因为第一个总线中的总线为空
解:
var number = function(busStops){
var total = 0;
for (var i=0; i<busStops.length; i++) {
total += busStops[i][0];
total -= busStops[i][1];
}
return total;
【8 kyu】Return the day
Complete the function which returns the weekday according to the input number:
1 returns "Sunday"
2 returns "Monday"
3 returns "Tuesday"
4 returns "Wednesday"
5 returns "Thursday"
6 returns "Friday"
7 returns "Saturday"
Otherwise returns "Wrong, please enter a number between 1 and 7"
翻译:
根据输入的数字完成返回工作日的函数:
1返回“星期日”
2返回“星期一”
3返回“星期二”
4返回“星期三”
5返回“星期四”
6返回“星期五”
7返回“星期六”
否则返回“错误,请输入1到7之间的数字”
解一:
function whatday(num) {
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return days[num - 1] || 'Wrong, please enter a number between 1 and 7';
}
解二:
function whatday(num) {
switch(num) {
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 7:
return "Saturday";
default:
return 'Wrong, please enter a number between 1 and 7';
}
}
【8 kyu】Define a card suit
You get any card as an argument. Your task is to return the suit of this card (in lowercase).
Our deck (is preloaded):
('3♣') -> return 'clubs'
('3♦') -> return 'diamonds'
('3♥') -> return 'hearts'
('3♠') -> return 'spades'
翻译:
你得到任何一张卡片作为论点。你的任务是归还这张牌的花色(小写)。
解一:
function defineSuit(card) {
if(card.includes('♥')) return 'hearts'
if(card.includes('♦')) return 'diamonds'
if(card.includes('♣')) return 'clubs'
if(card.includes('♠')) return 'spades'
}
解二:
function defineSuit(card) {
return {
'♣' : 'clubs',
'♦' : 'diamonds',
'♥' : 'hearts',
'♠' : 'spades'
}[card.slice(-1)]
}