Skip to content

Instantly share code, notes, and snippets.

@ycmjason
Created February 26, 2020 16:26
Show Gist options
  • Save ycmjason/a228ea13119fa1544f7f4b2a8c887c14 to your computer and use it in GitHub Desktop.
Save ycmjason/a228ea13119fa1544f7f4b2a8c887c14 to your computer and use it in GitHub Desktop.
import { reactive, watch } from '@vue/runtime-core'
// % in Javascript is remainder operator, e.g. -1 % 5 gives -1.
// The following is an implementation for modulus.
const mod = (x: number, y: number) => ((x % y) + y) % y
const MAX_ROAD_LENGTH = 10
const car = reactive({
position: 0,
speed: 2,
})
setInterval(() => {
car.position = mod(car.position + car.speed, MAX_ROAD_LENGTH)
}, 1000)
watch(() => {
const road = [...'_'.repeat(MAX_ROAD_LENGTH)]
road[car.position] = '🚗'
console.clear()
console.log(road.reverse().join(''))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment