简体   繁体   中英

JavaScript: How to display time using a switch statement?

I want to generate a custom time-formatted string via a switch statement where the date information is passed in an array.

For an array like this:

let displaySumTimes = ['00', '00', '10']

the expected output would be:

10sec

If a value in the array is greater than zero, then that should be included in the formatted string that is returned from my switch statement. Each non-zero value in the result also needs to have the corresponding time unit included with it in the formatted string result.

My current code looks like this:

 let displaySumTimes = ['00', '00', '10'];

 const formatTime = (time) => {
  const [hour, minute, sec] = time.split(':');
  console.log([hour, minute, sec]);
  switch([hour, minute, sec]) {
    case hour > 0:
      return `${hour} h ${minute} min ${sec} sec`;
      break;
    case minute > 0:
      return `${minute} min ${sec} sec`;
      break;
    case minute < 1:
      return `${sec} sec`;
    default:
      // code block
  }  
}

 formatTime(displaySumTimes); //output 10sec

 let displaySumTimes1 = ['00', '10', '10'];


 formatTime(displaySumTimes1); //output 10min 10 sec

It looks like your switch syntax is incorrect. You are not destructuring there, you're making a switch on a new array (which you're creating there in the conditional). Why not simply refactor your cases to be ifs?

Also, you're using a string method ( split ) on an array. I don't see any strings with ":"s in your code. If you're going to be using an array which you know will look like that ['##', '##', '##'] , you could use ifs like this:

if (hour !== "00") return `${hour} h ${minute} min ${sec} sec`;
if (minute !== "00") return `${minute} min ${sec} sec`;
return `${sec} sec`;

Perhaps you could take a functional approach to this which would avoid the need for a switch statement.

For instance, you could dynamically build a list containing each part of the formatted string result (which is built based on the input values), and then join() that list a white-space character to compose a string with the required format:

 const formatTime = (time) => { const [h, m, s] = time; /* Dynamically build a temporary list containing each part of the formatted time depending on input values supplied*/ return [].concat( h > 0 ? [`${ h }hr`] : [], m > 0 ? [`${ m }min`] : [], s > 0 ? [`${ s }sec`] : []) /* Join each part of the formatted time with a whitespace to achieve required formatting */ .join(' '); } let displaySumTimes = ['00', '00', '10']; let displaySumTimes1 = ['00', '10', '10']; console.log(formatTime(displaySumTimes)); //output 10sec console.log(formatTime(displaySumTimes1)); //output 10min 10 sec 

Interesting...

In your function switch will compare reference on array [hour, minute, sec] with boolean values in each cases (hour > 0, minute > 0 and minute < 1). It is not working...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM