Day 21 of Scrimba JavaScriptmas — Valid Time

Handhika Yanuar Pratama
2 min readDec 21, 2020

Here we go again, this is the 21/24 days of #javaScriptmas of #Scrimba challenge. Do you ever believe if you try as much as you can, someday you will somehow knowing that you are better than yesterday. It can’t be believed, but i feel this thing. For almost 6 months, today 21 December 2020, i can code by myself. Yeah i understand this is too early to say that, but it makes me happy, when i can solve problem by just using hints and little search for the hints, not the answer. This is my principal.

Once i dive into programming, this is the place that there is no ends on it. I’m not afraid of it, it makes me feel strong otherwise.

So this title of challenge is valid time, from the question you can take the meaning right(?). It is fun challenge, we just need to make sure that time format is true.

There is 3 variable to be checked, there are :

  • const str = ‘13:58’;
  • const str = ‘25:51’;
  • const str = ‘02:76’;

The hint from this challenge is using split() and parseInt() function. After a few research, split() is for splitting string into array and parseInt() is for change a variable into integer.

The first idea that come into my mind is that string can be automatically initialized as a integer. So by using a for loop, i just run inside the array of str and inside it i check the value. Before i use this statement.

if (0 <= str2[0] <= 24 && 0 <= str2[1] <= 60)

But as you know, the program don’t do anything and just give the true value, so after thinking for while, i realize that it’s a time variable, so we don’t need to make sure that the number is less than 0. After realizing it, i change the statement into this.

if(str2[0]<=24 && str2[1]<=60)

And yeah it’s done

So this is my full solution.

function validTime(str) {
// write code here.
let str2 = str.split(':')
let valid = true

for(i=0;i<str2.length;i++){
if(str2[0]<=24 && str2[1]<=60){
return valid
}else{
return !valid
}
}
}

And here is the link of my answered challenge in scrimba

Hope it can help, happy coding!!!

--

--