Personal Javascript Code Example – 01 – My first long Javascript code
This code was expanded upon a lesson by CodewithMosh. His code was very short. I added things to mine. Basic military time to standard time.
//Control flow with if and else
//If and else example
//Hour
//If hour is between 6am and 12pm: Good morning
//If it is between 12pm and 6pm: Good afternoon
//Otherwise: Good evening
for (let i = 0; i < 5; i++) {
let hour = Math.floor(Math.random() * 24); //Random number from 0 to 24
if (hour>6 && hour<12) {
console.log(‘Good Morning dude it is’, hour , ‘hours military time.’);
standardTime = hour + 0;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 AM’);
}
else if (hour>12 && hour<18) {
console.log(‘Good afternoon dude it is’, hour, ‘hours military time.’);
standardTime = hour – 12;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 PM’);
}
else if (hour<6) {
console.log(‘Good evening dude it is’, hour, ‘hours military time.’);
standardTime = hour;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 PM’);
}
else if (hour>=19 && hour<24) {
console.log(‘Good evening dude it is’, hour, ‘hours military time.’);
standardTime = hour – 12;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 PM’);
}
else if (hour=24) {
console.log(‘Good evening dude it is’, hour, ‘hours military time.’);
standardTime = hour – 23;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 PM’);
}
else if (hour=0) {
console.log(‘Good evening dude it is’, hour, ‘hours military time.’);
standardTime = ‘Midnight’;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 PM’);
}
else if (hour=12) {
console.log(‘Good evening dude it is’, hour, ‘hours military time.’);
standardTime = ‘Noon’;
console.log(‘Which is in standard time:’ , standardTime, ‘:00 PM’);
}
}