freeCodeCamp/curriculum/challenges/italian/10-coding-interview-prep/rosetta-code/comma-quibbling.md

2.7 KiB

id title challengeType forumTopicId dashedName
596e414344c3b2872167f0fe Cavillare 5 302234 comma-quibbling

--description--

Cavillare è un compito originariamente creato da Eric Lipper sul suo blog.

--instructions--

Scrivi una funzione per generare una stringa output che è la concatenazione di parole di input da una lista/sequenza dove:

  1. Un input senza parole produce una stringa output con solo due parentesi graffe ("{}")
  2. Un input di una sola parola, per esempio ["ABC"] produce una stringa output con la parola dentro due parentesi graffe ("{ABC}")
  3. Un input di due parole, per esempio ["ABC", "DEF"] produce una stringa output con due parole dentro le due parentesi graffe con le parole separate dalla stringa " and ", per esempio ("{ABC and DEF}")
  4. Un input di tre o più parole, per esempio ["ABC", "DEF", "G", "H"] produce una stringa output con tutte le parole, tranne le ultime due, separate da ", ", e con l'ultima parola separata da " and "; per esempio ("{ABC, DEF, G and H}")

Testa la tua funzione con la seguente serie di input mostrando il tuo output qui sulla pagina:

  • [] # (Nessuna parola di input).
  • ["ABC"]
  • ["ABC", "DEF"]
  • ["ABC", "DEF", "G", "H"]

Nota: Assumi che le parole siano stringhe non vuote di caratteri maiuscoli per questa sfida.

--hints--

quibble dovrebbe essere una funzione.

assert(typeof quibble === 'function');

quibble(["ABC"]) dovrebbe restituire una stringa.

assert(typeof quibble(['ABC']) === 'string');

quibble([]) dovrebbe restituire "{}".

assert.equal(quibble(testCases[0]), results[0]);

quibble(["ABC"]) dovrebbe restituire "{ABC}".

assert.equal(quibble(testCases[1]), results[1]);

quibble(["ABC", "DEF"]) dovrebbe restituire "{ABC and DEF}".

assert.equal(quibble(testCases[2]), results[2]);

quibble(["ABC", "DEF", "G", "H"]) dovrebbe restituire "{ABC,DEF,G and H}".

assert.equal(quibble(testCases[3]), results[3]);

--seed--

--after-user-code--

const testCases = [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]];
const results = ["{}", "{ABC}", "{ABC and DEF}", "{ABC,DEF,G and H}"];

--seed-contents--

function quibble(words) {

  return true;
}

--solutions--

function quibble(words) {
  return "{" +
    words.slice(0, words.length - 1).join(",") +
   (words.length > 1 ? " and " : "") +
   (words[words.length - 1] || '') +
  "}";
}