I've got a script which takes user input from a HTML form, and then defines some js variables, but the variables don't seem to be defining when I try to turn the "minute" and "hour" strings into integers.
I have the script output the variables into the console after the user inputs them, and that's working fine, but after I try to turn them into integers and output them into the console again, I just receive the "undefined" message.
//this is where the variables are output as strings -- this works
console.log(dropHour);
console.log(dropMinute);
//set hour and minute strings to integers -- doesn't work?
var Hour = parseInt(dropHour);
var Minute = parseInt(dropMinute);
console.log(dropMinute.type);
console.log(dropHour.type);
Or is there a better way to do this? Like have the input as numbers only and be a string from the get-go? I basically just need the user to be able to select a time and then have a function check that the user selected time is the same as the current time (which is done using this function, which doesn't work because of my above issue, i presume)
//checks if time set = current time
function checkTime() {
var currentTime = new Date();
console.log(currentTime.getHours);
if ((currentTime.getHours == Hour) && (currentTime.getMinutes == Minute)) {
console.log("time set = time now");
console.log(currentTime.getHours + currentTime.getMinutes);
setInterval(otherFunction(), 10);
}
}
I'm not sure I understand this code:
var Hour = parseInt(dropHour);
var Minute = parseInt(dropMinute);
console.log(dropMinute.type);
console.log(dropHour.type);
You're creating Hour
and Minute
which are numbers, but then looking at dropHour
and dropMinute
again (parseInt
does not change a variable, it returns a new value). Not to mention .type
isn't quite right. Code like this is probably more what you're looking for:
console.log(dropHour);
console.log(dropMinute);
var Hour = parseInt(dropHour, 10);
var Minute = parseInt(dropMinute, 10);
console.log(typeof Hour);
console.log(typeof Minute);
Here's a working example: https://jsbin.com/mekilu/edit?js,output
In addition to using the correct variable, I also changed parseInt(...)
to parseInt(..., 10)
as it's a good idea to always specify a radix, and Hour.type
and Minute.type
to typeof Hour
and typeof Minute
.