I'm trying to produce a date and time script from JS beginners, I'm sure some of you have seen this before.
I'm not sure where I've gone wrong and am struggling to problem solve it as the log just spits out 'error'.
Can anybody clear this up for me, sticking to my general way of writing the script. Explanations would be great!
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday];
var today = weekdays[day];
var amPm = amPmFunc;
function amPmFunc() {
if(hour < 12) {
amPm = AM;
} else {
amPm = PM;
}
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPM + ":" + min + ":" + "sec");
5 Answers 5
First of all, you forgot to enclose some strings with ''. i.e. Sunday should be 'Sunday'.
Also - remember - Javascript is case sensetive. So a variable called amPm will not be recognized as amPM.
Also, your function amPmFunc should return a value, rather than set a global variable's value. It's just neater this way. And when you do call it, call it with brackets: var amPm = amPmFunc();.
But other than that - Good job! ☻
See this fixed example:
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var today = weekdays[day];
var amPm = amPmFunc();
function amPmFunc() {
if(hour < 12)
return 'AM';
else
return 'PM';
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + "sec");
2 Comments
For why?
Arraydeclaration was wrong .Missed the quotes to formatting the string in array- Javascript is case sensitive one.so don't forget case of the letter
- For function declare you could use
()likevar amPm = amPmFunc().And add the return statement onampmfunction otherwise its returning undefined .
MY suggestion :For better you good study how to declare the variable , function and string in js
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var today = weekdays[day];
var amPm = amPmFunc()
function amPmFunc() {
if (hour < 12) {
amPm = 'AM';
} else {
amPm = 'PM';
}
return amPm
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + "sec");
Comments
var weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday];
They should be strings.
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Comments
- All strings must be in "
- name_of_function(); instead name_of_function
- Functions must return somethings, yours amPmFunc() was not returning amPm
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = weekdays[day];
function amPmFunc() {
if(hour < 12) {
amPm = "AM";
} else {
amPm = "PM";
}
return amPm;
}
var amPM = amPmFunc();
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPM + ":" + min + ":" + "sec");
Try this
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = weekdays[day];
var amPm = '';
if(hour < 12) {
amPm = 'AM';
} else if (hour > 12){
hour = hour - 12;
amPm = 'PM';
} else {
amPm = 'PM'
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + sec);
AM&PMbe in quotes too?