I have got a task to solve.
- On load, page should show 10 fields marked as A, B, C, D .... with value 3.
- After page load, every 2 seconds all field values should be changed randomly. Change is randomly calculated as a number between 1 and 2(1.98, 1.02...), with a random sign (-, +).
At this point, I was able to set the initial value and solve the random floating numbers, but I am struggling to sum initialValue
and randomNumber
because of floating points. I was able to do it properly with whole numbers.
addition()
function return 31.58 instead of 4.58, which is I think right JS behavior, but I don't know how to fix it.
Here is the code so far:
<template>
<div class="table-fields">
<table>
<tr>
<th>A</th>
<td>{{ initialValue }}</td>
</tr>
</table>
<button @click="stopInterval()">Stop</button>
</div>
</template>
<script>
export default {
name: 'TableFields',
props: {
msg: String
},
data: function () {
return {
initialValue: 3,
randomNumber: (Math.random() * 1 + 1).toFixed(2),
interval: setInterval(this.addition, 2000)
}
},
computed: {
},
methods: {
addition () {
this.initialValue += this.randomNumber
},
stopInterval () {
clearInterval(this.interval)
}
},
mounted () {
// const test = this.initialValue
console.log(this.randomNumber)
}
}
</script>