Code Lab

Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado

Lucky ticket

MediaBasics10 min

Write a function isLuckyTicket(ticket) that checks if a bus ticket number is lucky.

  • ticket is a string.
  • The ticket is lucky when it has exactly 6 characters, every character is a digit from 0 to 9, and the sum of the first three digits equals the sum of the last three digits. '123321' is lucky: 1 + 2 + 3 = 6 and 3 + 2 + 1 = 6.
  • Return a boolean: true for a lucky ticket, false otherwise.
  • Any string that is not exactly 6 digits gives false: a shorter or longer string, the empty string, a string with letters or spaces.

Careful with +: '1' + '2' is '12', not 3. With a string, + means concatenation, and type coercion turns 0 + '1' into '01'. Turn every digit into a number before you add it.

Ejemplos

isLuckyTicket('123321')

Devuelve true

isLuckyTicket('123456')

Devuelve false

1 + 2 + 3 = 6, but 4 + 5 + 6 = 15.
isLuckyTicket('12345')

Devuelve false

Only 5 digits.

Fuente

Cargando el editor…
  1. EntradaisLuckyTicket('123321')
    Esperadotrue
  2. EntradaisLuckyTicket('123456')
    Esperadofalse
  3. EntradaisLuckyTicket('12345')
    Esperadofalse

Las pruebas ocultas se ejecutan al enviar