Skip to content

Instantly share code, notes, and snippets.

@zbarnes757
Last active January 4, 2018 20:51
Show Gist options
  • Save zbarnes757/fca07b08aeaf540778361e19191ec4b6 to your computer and use it in GitHub Desktop.
Save zbarnes757/fca07b08aeaf540778361e19191ec4b6 to your computer and use it in GitHub Desktop.
javascript clock
defmodule Clock do
def start do
time = {12, 0, 0}
print_time(time)
:timer.sleep(1_000)
increment_time(time)
end
defp increment_time(time) do
time = update_seconds(time)
print_time(time)
:timer.sleep(1_000)
increment_time(time)
end
defp print_time({hours, minutes, seconds}) do
IO.puts "#{stringify_time(hours)}:#{stringify_time(minutes)}:#{stringify_time(seconds)}"
end
defp stringify_time(time) when time >= 10, do: "#{time}"
defp stringify_time(time), do: "0#{time}"
defp update_seconds({hours, minutes, 59}), do: update_minutes({hours, minutes, 0})
defp update_seconds({hours, minutes, seconds}), do: {hours, minutes, seconds + 1}
defp update_minutes({hours, 59, seconds}), do: update_hours({hours, 0, seconds})
defp update_minutes({hours, minutes, seconds}), do: {hours, minutes + 1, seconds}
defp update_hours({12, minutes, seconds}), do: update_hours({1, minutes, seconds})
defp update_hours({hours, minutes, seconds}), do: {hours + 1, minutes, seconds}
end
Clock.start()
class Clock {
constructor() {
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
static stringifyTime(timeUnit) {
if (timeUnit >= 10) {
return '' + timeUnit;
}
else {
return '0' + timeUnit;
}
}
start() {
this.printTime();
setTimeout(() => this.incrementTime(), 1000);
}
incrementTime() {
this.updateSeconds();
this.printTime();
setTimeout(() => this.incrementTime(), 1000);
}
updateSeconds() {
if (this.seconds === 59) {
this.seconds = 0;
this.updateMinutes();
} else {
this.seconds++;
}
}
updateMinutes() {
if (this.minutes === 59) {
this.minutes = 0;
this.updateHours();
} else {
this.minutes++;
}
}
updateHours() {
if (this.hours == 24) {
this.hours = 1;
} else {
this.hours++;
}
}
printTime() {
const stringHours = Clock.stringifyTime(this.hours);
const stringMinutes = Clock.stringifyTime(this.minutes);
const stringSeconds = Clock.stringifyTime(this.seconds);
console.log(stringHours + ':' + stringMinutes + ':' + stringSeconds);
}
}
const clock = new Clock();
clock.start();
class Clock
def initialize
@hours = 12
@minutes = 0
@seconds = 0
end
def start
print_time
sleep 1
increment_time
end
private
def increment_time
update_seconds
print_time
sleep 1
increment_time
end
def stringify_time(time_unit)
if time_unit >= 10
"#{time_unit}"
else
"0#{time_unit}"
end
end
def print_time
string_hours = stringify_time(@hours)
string_minutes = stringify_time(@minutes)
string_seconds = stringify_time(@seconds)
puts "#{string_hours}:#{string_minutes}:#{string_seconds}"
end
def update_seconds
if @seconds == 59
@seconds = 0
update_minutes
else
@seconds += 1
end
end
def update_minutes
if @minutes == 59
@minutes = 0
update_hours
else
@minutes += 1
end
end
def update_hours
if @hours == 12
@hours = 0
else
@hours += 1
end
end
end
clock = Clock.new
clock.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment