Skip to content Skip to sidebar Skip to footer

I Wonder What This Function Really Does .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 sam

Solution 1:

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

Solution 2:

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 numberconst N = +readline();
// Line 2: reads string of temperatures.// tC contains an array of temperatures of length N in numbers. + converts to numberconst tC = readline().split(' ').map(x => +x);

let min = Infinity;
// iterate over tC arrayfor (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 ALTERNATIVEconst 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 ALTERNATIVEconst tC = "1 -2 -8 4 5".split(' ').map(x => +x);

let min = Infinity;
// iterate over tC arrayfor (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);

Solution 3:

functionreadLine(){
  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);

Post a Comment for "I Wonder What This Function Really Does .split(' ').map(x => +x);"