1

I created this function in my application:

const test = (seconds) => {
  let dateType = "seconds";
  let time = seconds;

  if (seconds % 60 === 0) {
    dateType = "minutes";
    time = seconds / 60;
  }

  if (seconds % 3600 === 0) {
    dateType = "hours";
    time = seconds / 3600;
  }

  if (seconds % 86400 === 0) {
    dateType = "days";
    time = seconds / 86400;
  }

  return {
    dateType,
    time,
  };
};

I want to change the function above into something like this:

const test = (seconds) => {
  let dateType = "seconds";
  let time = seconds;

  const OUTPUT = {
    [seconds % 60 === 0]: {
      dateType: "minutes",
      time: seconds / 60,
    },
    [seconds % 3600 === 0]: {
      dateType: "hours",
      time: seconds / 3600,
    },
    [seconds % 86400 === 0]: {
      dateType: "days",
      time: seconds / 86400,
    },
  };

  return OUTPUT[seconds];
};

console.log(test(60)); // here i need to get the first item from OUTPUT
How to change the last function, because now i get undefined?

7
  • perhaps the missing ' in dateType: 'minutes, is causing the code to not even parse Commented Jun 7, 2021 at 9:34
  • you do realise [seconds % 86400 === 0] will be true or false ... so the object will only ever have at most 2 properties, true, and false ... and never a numeric property ... like 60 in your example ... add console.log(OUTPUT); before you return in that function, and you'll see your problem Commented Jun 7, 2021 at 9:36
  • @GalAbra - that too would be undefined Commented Jun 7, 2021 at 9:37
  • @JaromandaX, how to change the code to get the expected result? Commented Jun 7, 2021 at 9:37
  • Use the function that works, not some arbitrary code that could never work Commented Jun 7, 2021 at 9:38

1 Answer 1

-1

There is no item in OUTPUT with the key seconds. The keys of the objects are conditions that become true or false.

So if you want to get the first item return the item that has the key 'true' if there is one, else return the item that has the key 'false'.

Working example:

const test = (seconds) => {
  let dateType = "seconds";
  let time = seconds;

  const OUTPUT = {
    [seconds % 60 === 0]: {
      dateType: "minutes",
      time: seconds / 60,
    },
    [seconds % 3600 === 0]: {
      dateType: "hours",
      time: seconds / 3600,
    },
    [seconds % 86400 === 0]: {
      dateType: "days",
      time: seconds / 86400,
    },
  };

  console.log('OUTPUT: ', OUTPUT);
  
  return OUTPUT['true'] ? OUTPUT['true'] : OUTPUT['false'];
};

console.log('60: ', test(60));
console.log('61: ', test(61));
console.log('3600: ', test(3600));
console.log('86400: ', test(86400));

Sign up to request clarification or add additional context in comments.

3 Comments

So, what happens when you pass in 61? hint: it's NOT the same as the first function
I doubt you'll find code that's very different to the first code in the question - let me rephrase that, of course you can write this in many different ways, some worse than others, I just can't see a significantly better way than the original
@Asking I updated my answer - please give a feedback...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.