Search code examples
javascriptdictionaryarrow-functions

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


Solution

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