Is Lucky

Description


Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.

Given a ticket number n, determine if it’s lucky or not.

Example

  • For n = 1230, the output should be isLucky(n) = true;
  • For n = 239017, the output should be isLucky(n) = false.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] integer n

A ticket number represented as a positive integer with an even number of digits.

Guaranteed constraints: 10 \leq n \le 10^6.

  • [output] integer

true if n is a lucky ticket number, false otherwise.

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
function isLucky(n) {
    return String(n).split('').map(Number).reduce((a,b)=>a+b)/2 === String(n).split('').slice(0,String(n).length/2).map(Number).reduce((a,b)=>a+b)
}