Code Lab

Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion

Middle three characters

MoyenStrings10 min

Write a function middleThree(text) that returns the three characters in the middle of the string text: 'NewMumbai' gives 'Mum'.

  • text is plain Latin text from 0 to 1000 characters. Every character counts, spaces and signs too, also at the start and the end. Do not trim the text.
  • If the length of text is 3 or less, return text as it is. For an empty string return ''.
  • If the length is odd and more than 3, return the middle three characters: the same number of characters stays on the left and on the right of them.
  • If the length is even and more than 3, there is no single middle. Return null, not ''.

You can cut the part out with slice.

Exemples

middleThree('NewMumbai')

Renvoie 'Mum'

middleThree('abcd')

Renvoie null

Even length: no single middle.
middleThree('ab')

Renvoie 'ab'

Posé chez EPAM · Source

Chargement de l'éditeur...
  1. EntréemiddleThree('NewMumbai')
    Attendu'Mum'
  2. EntréemiddleThree('abcd')
    Attendunull
  3. EntréemiddleThree('ab')
    Attendu'ab'

Les tests cachés se lancent à la soumission