-
-
Save ycmjason/bf1c13ee60912e2d387b00b1e7ad7dba to your computer and use it in GitHub Desktop.
This file contains 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
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