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

183
Visualizações
Adding up Switch Case Values

This might be really simple, so I do apologise but I can't find any other question asking the same/similar. Here goes...

I am trying to find a way to add up all the case values used in each switch. So to calculate all the days of the week numbers and output the total numbers in the following

const getSleepHours = (day) => {
  switch(day) {
    case "Monday":
      return "8";
    break;

    case "Tuesday":
      return "9";
    break;
    
    case "Wednesday":
      return "9.5";
    break;
    
    case "Thursday":
      return "11";
    break;
    
    case "Friday":
      return "10.5";
    break;
    
    case "Saturday":
      return "13";
    break;
    
    case "Sunday":
      return "12";
    break;

    default:
      return "Not sure what day that is";
  }
};

So 8 + 9 + 9.5 + 11 + 10.5 + 13 + 12. So far I have tried things like the following

const getSleepHours = (day) => {
  switch(day) {
    case "Monday":
      return "8";
    break;

    case "Tuesday":
      return "9";
    break;
    
    case "Wednesday":
      return "9.5";
    break;
    
    case "Thursday":
      return "11";
    break;
    
    case "Friday":
      return "10.5";
    break;
    
    case "Saturday":
      return "13";
    break;
    
    case "Sunday":
      return "12";
    break;

    default:
      return "Not sure what day that is";
  }
};


const getActualSleepHours = () => {
  const getSleepHours = ("Monday","Sunday");
    return getSleepHours();
};
console.log(getSleepHours());

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could get the value for each day of the week, parse it as a float number. Then use those values to calculate the total (for example by using reduce()).

Also if you return out of a case, you don't need to break because it is no longer reachable.

const getSleepHours = (day) => {
  switch (day) {
    case "Monday":
      return "8";
    case "Tuesday":
      return "9";
    case "Wednesday":
      return "9.5";
    case "Thursday":
      return "11";
    case "Friday":
      return "10.5";
    case "Saturday":
      return "13";
    case "Sunday":
      return "12";
    default:
      return "Not sure what day that is";
  }
};

const days = [
  "Monday", "Tuesday", "Wednesday", 
  "Thursday", "Friday", "Saturday", "Sunday"
];

// get hours as float values
const sleepHours = days.map((d) => parseFloat(getSleepHours(d)));
// calculate the total
const sum = sleepHours.reduce((a, b) => a + b, 0);

console.log(sum);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do without switch-case

// 1️⃣
// Using Map object
// If you define duplicated key It won's so error
const sleepHours = new Map([
  ['monday', 8],
  ['tuesday', 9],
  ['wednesday', 9.5],
  ['thursday', 11],
  ['friday', 10.5],
  ['saturday', 13],
  ['sunday', 12],
])

let totalSleepHours = 0
for (const [key, value] of sleepHours) {
  totalSleepHours += value
}

// 2️⃣
// Using object
const sleepHours = {
  monday: 8,
  tuesday: 9,
  wednesday: 9.5,
  thursday: 11,
  friday: 10.5,
  saturday: 13,
  sunday: 12,
}

// If not provide initial value, array element at index 0 is used as the initial value
Object.values(sleepHours).reduce((acc, curr) => acc += curr)
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