I wonder what this function really does .split(' ').map(x => +x);

喜欢而已 提交于 2019-12-24 10:59:12

问题


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

but the rest of the code didn't work

Context :

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

Thanks a lot


回答1:


According to this site below are the inputs the program should receive

Line 1: N, the number of temperatures to analyze

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

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



回答2:


The .map(x => +x) converts all items in the array to a number. And returns a new array with those converted values.

If you change it to .map(x => x) then the values are left untouched und you just create a copy of the original array. So the strings remain strings which will break the code if numbers are expected.

I personally would avoid the +x syntax and use the more verbose Number(x), and write either .map(x => Number(x)) or .map(Number).




回答3:


function readLine(){
  return "123456"
}

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

console.log(result)

readLine().split("") // splits the string into an array as follows ["1", "2", "3", "4", "5", "6"]

.map(x => +x) // map method returns a new array which will take each item and gives a new array , here number changing from string to numbers as follows [1, 2, 3, 4, 5, 6] since +x is used

// the above is written in es6, which can be re written in es5 as follows

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

// Note

In es6 if we have a single thing to pass we can avoid the function(x) to x

also we can remove the {} [curly braces and return too] {return +x} to +x

ES2015

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

ES2016

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


来源:https://stackoverflow.com/questions/52702610/i-wonder-what-this-function-really-does-split-mapx-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!