-1

beginning in coding, I'm trying to build a function that asks the user to enter a value for hour then for minutes and return an array showing clock one minute later, but it isn't working I tried to fix it for some hours now and am really stuck, could anyone tell me where am wrong please?

let hour = window.prompt("Enter hour", ""); 
let minutes = window.prompt("Enter minutes", "");

function clock(hour, minutes) {  
let arr = [];
  if(0 <= hour < 23 && 0 <= minutes < 59) {
      arr.push(hour, minutes + 1);
  } else if((minutes == 59) && (hour == 23)){
           arr.push(0, 0);
         }
         return arr;
        }
    clock();
console.log(clock(23, 59));// Got [23, 60] ❌ should return [0, 0]
console.log(clock(15, 3));// Got [15, 4] ✅

Thanks for answering, here is the clean code

function clock(hour, minutes) {  
let arr = [];
  if(0 <= minutes && minutes < 59) {
      arr.push(hour, minutes + 1);
  } else if(minutes == 59){
    arr.push(hour + 1, 0);
    if(hour == 23){
      arr.push(0, 0);
    }
  }
    return arr;
        }
    clock();
   console.log(clock(13, 27)); // returns [13, 28];
   console.log(clock(13, 59)); // returns [14, 0];
OxDimm IT
  • 3
  • 2
  • Besides the wrong check if the value is between a certain range, using values 13, 59 would still return 13, 60 because the hour is not set to 23. – Enak Apr 22 '22 at 03:59
  • Geez you're right I'm working on it, thank you amigo – OxDimm IT Apr 22 '22 at 04:20

1 Answers1

0

I don't think you can do this: 0 <= hour < 23 && 0 <= minutes < 59

It should be: 0 <= hour && hour < 23 && 0 <= minutes && minutes < 59

xubury
  • 84
  • 6
  • Thanks a lot, it works don't know why I can't do this but it seemed logic to me. – OxDimm IT Apr 22 '22 at 04:05
  • @OxDimmIT Compare operators just takes two arguments and return a boolean. This means e.g. for hour = 15 the following happens: `0 <= 15 < 23` is equal to `True < 23` is equal to `True` because `True` is numerical a `1` – Enak Apr 22 '22 at 04:14