我想知道这个函数到底是做什么的.split('').map(x => + x); [英] I wonder what this function really does .split(' ').map(x => +x);
本文介绍了我想知道这个函数到底是做什么的.split('').map(x => + x);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在编码游戏的更正中看到了这行代码
I saw this line of code in a correction in a coding game
const tC = readline().split(' ').map(x => +x);
我想知道它是做什么的,因为当我登录此功能时,它会呈现与此功能相同的东西
I wonder what it does because when I log this function it render the same thing that this one
const tC = readline().split(' ').map(x => x);
但其余代码无效
上下文:
/** Temperatures (easy) https://www.codingame.com/training/easy/temperatures
* Solving this puzzle validates that the loop concept is understood and that
* you can compare a list of values.
* This puzzle is also a playground to experiment the concept of lambdas in
* different programming languages. It's also an opportunity to discover
* functional programming.
*
* Statement:
* Your program must analyze records of temperatures to find the closest to
* zero.
*
* Story:
* It's freezing cold out there! Will you be able to find the temperature
* closest to zero in a set of temperatures readings?
**/
const N = +readline();
const tC = readline().split(' ').map(x => +x);
let min = Infinity;
for (let i in tC) {
(Math.abs(tC[i]) < Math.abs(min) || tC[i] === -min && tC[i] > 0) && (min = tC[i]);
}
print(min || 0);
非常感谢
推荐答案
根据此网站以下是程序应接收的输入
According to this site below are the inputs the program should receive
第1行:N,要分析的温度数
Line 1: N, the number of temperatures to analyze
第2行:N个温度表示为-273至5526范围内的整数的字符串
Line 2: A string with the N temperatures expressed as integers ranging from -273 to 5526
让我逐行提供有关游戏规则的评论
Let me provide line by line comments with respect to the game rules
// Line 1: reads number temperature inputs. + converts to number
const N = +readline();
// Line 2: reads string of temperatures.
// tC contains an array of temperatures of length N in numbers. + converts to number
const tC = readline().split(' ').map(x => +x);
let min = Infinity;
// iterate over tC array
for (let i in tC) {
// If two numbers are equally close to zero, positive integer has to be considered closest to zero
// set min = current iterating number if it matches above condition
(Math.abs(tC[i]) < Math.abs(min) || tC[i] === -min && tC[i] > 0) && (min = tC[i]);
}
print(min || 0);
这是使用javascript的有效演示
进行了修改,以使初学者可以理解.
Here is the working demo in javascript
modified to make it understandable for beginners.
// Line 1: reads number temperature inputs. + converts to number
// const N = +readline(); SAMPLE ALTERNATIVE
const N = +"5";
// Line 2: reads string of temperatures.
// tC contains an array of temperatures of length N in numbers. + converts to number
// const tC = readline().split(' ').map(x => +x); SAMPLE ALTERNATIVE
const tC = "1 -2 -8 4 5".split(' ').map(x => +x);
let min = Infinity;
// iterate over tC array
for (let i in tC) {
// If two numbers are equally close to zero, positive integer has to be considered closest to zero
// set min = current iterating number if it matches above condition
(Math.abs(tC[i]) < Math.abs(min) || tC[i] === -min && tC[i] > 0) && (min = tC[i]);
}
console.log(min || 0);
这篇关于我想知道这个函数到底是做什么的.split('').map(x => + x);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文