Created
October 28, 2021 16:43
-
-
Save milksense/ef0b8c2fcc7b29fcdaad1c57c56a34fe to your computer and use it in GitHub Desktop.
Display greeting by time via TS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const greeting = () => { | |
const day = new Date(); | |
const hour = day.getHours(); | |
let result = null; | |
if (hour >= 5 && hour < 12) { | |
result = "Good morning"; | |
} | |
else { | |
if (hour >= 12 && hour < 18) { | |
result = "Good afternoon"; | |
} | |
else { | |
if (hour >= 18 && hour < 24) { | |
result = "Good evening"; | |
} | |
else { | |
if (hour >= 0 && hour < 5) { | |
result = "Good night"; | |
} | |
} | |
} | |
} | |
return result; | |
}; | |
console.log(greeting()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Greeting = () => string | null; | |
type Result = string | null; | |
const greeting: Greeting = () => { | |
const day: Date = new Date(); | |
const hour: number = day.getHours(); | |
let result: Result = null; | |
if (hour >= 5 && hour < 12) { | |
result = "Good morning"; | |
} else { | |
if (hour >= 12 && hour < 18) { | |
result = "Good afternoon"; | |
} else { | |
if (hour >= 18 && hour < 24) { | |
result = "Good evening"; | |
} else { | |
if (hour >= 0 && hour < 5) { | |
result = "Good night"; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
console.log(greeting()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment