Code Lab

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

Files in one folder

DifficileJSON and data15 min

Write a function filesInFolder(paths, folder) that returns the names of the files that are right in one folder.

  • paths is an array of file paths of a project, like the output of git ls-files: folder names and the file name joined with /, for example 'src/utils/math.js'. A path never starts with /. The same path can occur more than once.
  • folder is a string: a folder path from the project root, such as 'src' or 'src/utils'. 'src/' (with a slash at the end) means the same as 'src'. The empty string '' means the project root.
  • Return an array of file names (the last part of a path, without folders). Take only files that are right in folder, not files in its subfolders. 'src2' is a different folder from 'src'.
  • Every name is in the result once.
  • Sort the names with the default sort(): it compares strings by character codes, so 'README.md' comes before 'app.js'.
  • If no file is right in folder (also when there is no such folder or paths is empty), return an empty array.

You can split a path by / to get its parts.

Exemples

filesInFolder(['src/app.js', 'src/utils/math.js', 'README.md', 'src/index.html', 'src2/x.js'], 'src')

Renvoie [ 'app.js', 'index.html' ]

filesInFolder(['src/app.js', 'src/utils/math.js', 'README.md'], 'src/utils')

Renvoie [ 'math.js' ]

filesInFolder(['src/app.js', 'README.md'], 'docs')

Renvoie []

Posé chez ServiceNow · Source

Chargement de l'éditeur...
  1. EntréefilesInFolder([ 'src/app.js', 'src/utils/math.js', 'README.md', 'src/index.html', 'src2/x.js' ], 'src')
    Attendu[ 'app.js', 'index.html' ]
  2. EntréefilesInFolder([ 'docs/intro.md', 'tests/login.spec.ts', 'tests/cart.spec.ts' ], 'tests')
    Attendu[ 'cart.spec.ts', 'login.spec.ts' ]
  3. EntréefilesInFolder([ 'src/app.js', 'README.md' ], 'docs')
    Attendu[]

Les tests cachés se lancent à la soumission