This is my unique text
<template>
<div class="unique_class">
<div class="unique_wrapper">
<div v-for="(h, index) in heights" :key="index" class="each_bar" v-bind:style="{ height: h + 'px' }"></div>
</div>
<div class="unique-buttons">
<button @click="resetArray">Reset</button>
<button @click="bubbleSort">Bubble Sort</button>
<button @click="sort">Sort</button>
</div>
</div>
</template>
Here's the script
export default {
name: 'UniqueComponent',
data() {
return {
heights: [],
totalBars: 100,
}
},
methods: {
getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
},
resetArray() {
this.heights = [];
for(let i=0; i<this.totalBars; i++) {
this.heights.push(this.getRandomInt(2, 400));
}
},
bubbleSort() {
for(let i=0; i<this.heights.length; i++) {
for (let j=0; j<(this.heights.length-i-1); j++) {
if(this.heights[j]>this.heights[j+1]) {
let temp = this.heights[j];
this.heights[j] = this.heights[j+1];
this.heights[j+1] = temp;
}
}
}
console.log(this.heights);
},
sort() {
this.heights.sort((a, b) => a-b);
console.log(this.heights);
},
},
mounted() {
for(let i=0; i<this.totalBars; i++) {
this.heights.push(this.getRandomInt(2, 400));
}
},
}
I have encountered an issue where clicking the sort
button correctly updates the template as expected, but when I click the bubbleSort
button, although the heights are sorted in the console, the changes are not reflected in the template. Can anyone assist me with resolving this issue?