-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];
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. Commented Apr 22, 2022 at 3:59
  • Geez you're right 🥵 I'm working on it, thank you amigo Commented Apr 22, 2022 at 4:20

1 Answer 1

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

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

2 Comments

Thanks a lot, it works 🙏 don't know why I can't do this but it seemed logic to me.
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.