Please post a link to the exercise page. Thanks.
- generate a secret number
- get a computer guess
- get a user guess
- compute distance from target for computer
- compute distance from target for user
- compare which absolute distance is smaller
- return a winner
const absDiff = (a, b) => Math.abs(b - a)
That is a utility function that can help us cut down on code, since it is repeated.
const generate = (low, high) => Math.random() * (high - low) + low
The above is a utility function to generate a random float within a specified range (lower bound, upper bound). We have left off any change to that return value.
const floor = x => Math.floor(x)
With the above two, we can now write a function to generate integers.
const randint = x => floor(generate(1, 10))
- target = randint(1, 10)
- computer = randint(1, 10)
- user = randint(1, 10)
- winner = absDiff(user, target) < absDiff(computer, target) ? ‘User wins!’ : ‘Computer wins.’
This is not solution code as much as something to think about and tinker with. Use it for a learning tool.