-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisLucky.js
22 lines (17 loc) · 810 Bytes
/
isLucky.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
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
solution(n) = true;
For n = 239017, the output should be
solution(n) = false.
true if n is a lucky ticket number, false otherwise.
*/
function solution(ticket){
const newTicket = ticket.toString().split('');
const ticketFirstHalf = newTicket.slice(0, newTicket.length /2);
const ticketSecondHalf = newTicket.slice( ticketFirstHalf.length ,newTicket.length);
return ticketFirstHalf.reduce((a,b)=>(+a)+(+b)) === ticketSecondHalf.reduce((a,b)=>(+a)+(+b));
}
console.log(solution(239017));