我想知道这个函数实际上是做什么的.split('').map(x => + x);

时间:2018-10-08 12:43:54

标签: javascript dictionary arrow-functions

我在编码游戏的更正中看到了这行代码

const tC = readline().split(' ').map(x => +x);

我想知道它是做什么的,因为当我登录此功能时,它会呈现与此功能相同的东西

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);

非常感谢

3 个答案:

答案 0 :(得分:1)

.map(x => +x)将数组中的所有项目转换为数字。并返回具有这些转换后值的新数组。

如果将其更改为.map(x => x),则这些值将保持不变,而您只需创建原始数组的副本即可。因此,字符串仍然是字符串,如果需要数字,则会破坏代码。

我个人会避免使用+x语法,而使用更冗长的Number(x),而写.map(x => Number(x)).map(Number)

答案 1 :(得分:1)

根据下面的site,是程序应接收的输入

第1行:N,要分析的温度数

第2行:N个温度的字符串表示为-273至5526之间的整数

让我逐行提供有关游戏规则的评论

// 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的有效演示

进行了修改,以使初学者可以理解。

// 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);

答案 2 :(得分:0)

function readLine(){
  return "123456"
}

var result = readLine().split("").map(x => +x)

console.log(result)

readLine().split("") // 将字符串按如下所示拆分为数组[“ 1”,“ 2”,“ 3”,“ 4”,“ 5”,“ 6”]

.map(x => +x) // map方法返回一个新数组,该数组将接受每个项目并提供一个新数组,此处数字从字符串更改为数字,如下所示[1、2、3, 4,5,6],因为使用了+ x

// 以上内容是用es6编写的,可以用es5编写,如下所示

readLine().split("").map(function(x) {
  return +x
})

// 注意

在es6中,如果只有一件事情要传递,我们可以避免将函数(x)传递给x

我们也可以删除{} [花括号并返回] {return +x}+x

ES2015

readLine().split("").map(function(x) {
  return +x
})

ES2016

readLine().split("").map(x => +x);