Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Javascript: How to add an optional forward slash to a template literal

So I am trying to have two optional arguments and an optional forward slash. The reason I need an optional forward slash is because the base url is just localhost:3000/web/reserve not localhost:3000/web/reserve/. The default empty arguments are working as expected when passing nothing into webpageCheck however I can't get the optional (/) to work. If I did http://localhost:3000/web/reserve/${label}${count} it would fail when calling webpageCheck because there is no forward slash at the end. Does anyone know how to do this?

const web = {
        label: 'Hello',
        count: '10',
    };
    
const webpageCheck = (label = '', count = '') => {
     const optionalSlash = '/'
      cy.url().should(
         'eq',
         `http://localhost:3000/web/reserve${optionalSlash + label}${optionalSlash + count}`
        );
    };
    
 webpageCheck()
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

So, it sounds like the optionalSlash should be in the string before label or count, and if label or count is missing, we skip adding the optionalSlash for that variable. If that's true, you could do a simple ternary when constructing the string.

`http://localhost:3000/web/reserve${
  label ? optionalSlash + label : ''
  }${
  count ? optionalSlash + count : ''}`
about 4 years ago · Juan Pablo Isaza Relatório

0

You could collect the parts in an array, filter it by boolean and join with wanted separator.

const
    web = { label: 'Hello', count: '10' },
    webpageCheck = (label = '', count = '', separator = '/') => 
        ['http://localhost:3000/web/reserve', label, count]
            .filter(Boolean)
            .join(separator);

console.log(webpageCheck());
console.log(webpageCheck(web.label));
console.log(webpageCheck(web.label, web.count));

about 4 years ago · Juan Pablo Isaza Relatório

0

You should try to define a string with your logic, something like this:

const web = {
        label: 'Hello',
        count: '10',
    };
    
const webpageCheck = (label = '', count = '') => {
     //Logic example
     let addressOptionsString=(label!==''? ("/" + label):'') + (count!==''? ("/" + count):'')
      cy.url().should(
         'eq',
         `http://localhost:3000/web/reserve${adressOptionsString}`
        );
    };
    
 webpageCheck()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda