refactor: add explanations to clock 4th exercise

This commit is contained in:
Inanc Gumus
2018-12-14 17:35:44 +03:00
committed by GitHub
parent 2e379339d7
commit ded0d0c603

View File

@@ -15,7 +15,6 @@ import (
) )
func main() { func main() {
// by using 'shift' we can create the slide effect for placeholders.
for shift := 0; ; shift++ { for shift := 0; ; shift++ {
// we need to clear the screen here. // we need to clear the screen here.
// or the previous character will be left on the screen // or the previous character will be left on the screen
@@ -39,24 +38,30 @@ func main() {
for line := range clock[0] { for line := range clock[0] {
l := len(clock) l := len(clock)
// this sets the beginning and the ending placeholders. // this sets the beginning and the ending placeholder positions (indexes).
// to prevent the indexing error: we use the remainder operator. // shift%l prevents the indexing error.
s, e := shift%l, l s, e := shift%l, l
// to slide placeholders from the right part of the screen. // to slide the placeholders from the right part of the screen.
// //
// here, we assume that as if the clock's length is double of its length. // here, we assume that as if the clock's length is double of its length.
// this makes things easy to manage: that's why: l*2 is there. // this makes things easy to manage: that's why: l*2 is there.
// //
// whenever, the current shift factor's double remainder is greater than // shift is always increasing, for it's to go beyond the clock's length,
// the length of the clock - 1, it changes the starting and ending positions. // it should be equal or greater than l*2, right (after the remainder of course)?
if shift%(l*2) > l-1 { //
s, e = 0, shift%l+1 // so, if the clock goes beyond its length; this code detects that,
// and resets the starting the position to the first placeholder's index,
// and it keeps doing so until the clock is fully displayed again.
if shift%(l*2) >= l {
s, e = 0, s
} }
// print empty lines for the right-to-left slide effect. // print empty lines for the missing place holders.
//
// this creates the effect of moving placeholders from right to left. // this creates the effect of moving placeholders from right to left.
//
// l-e can only be non-zero when the above if statement runs.
// otherwise, l-e is always zero, because l == e.
for j := 0; j < l-e; j++ { for j := 0; j < l-e; j++ {
fmt.Print(" ") fmt.Print(" ")
} }