Code Lab

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

Card type by the first four digits

FácilBasics5 min

Write a function cardType(cardNumber) that finds the card type from the first four digits of a card number.

  • cardNumber is a string of exactly 16 digits, without spaces. It can start with 0.
  • Add up the first four digits. If the sum is 16, return 'Visa', otherwise return 'Master'.
  • Return exactly 'Visa' or 'Master', with a capital first letter.

Why a string: a 16-digit card number does not always fit exactly into a JavaScript number. For example, Number('9999999999999999') gives 10000000000000000.

Ejemplos

cardType('4552720412345678')

Devuelve 'Visa'

4 + 5 + 5 + 2 = 16
cardType('5100123412341234')

Devuelve 'Master'

5 + 1 + 0 + 0 = 6
cardType('0790100000000000')

Devuelve 'Visa'

0 + 7 + 9 + 0 = 16

Preguntada en Luxoft · Fuente

Cargando el editor…
  1. EntradacardType('4552720412345678')
    Esperado'Visa'
  2. EntradacardType('5100123412341234')
    Esperado'Master'
  3. EntradacardType('0790100000000000')
    Esperado'Visa'

Las pruebas ocultas se ejecutan al enviar