I've got a form with 3 input values and I'm trying to sort them from smallest to highest and then do this.
I've got them sorted and pushed to the inputs array but I can't get my head around adding and multiplying them 😓.
Any help or sugestions guys.
<form class="calc__container">
<input
data-input
type="number"
id="length"
placeholder="Length">
<input
data-input
type="number"
id="width"
placeholder="Width">
<input
data-input
type="number"
id="hight"
placeholder="Higth">
<button class="submit__btn hover">Submit</button>
<div class="ans-container">
<p class="ans__p ">271 cm</p>
</div>
</form>
let lInput = document.getElementById('length');
let wInput = document.getElementById('width');
let hInput = document.getElementById('hight');
const submitBtn = document.querySelector('.submit__btn')
let inputs = []
submitBtn.addEventListener('click', function (e) {
e.preventDefault();
let allpointsArr = [
Number(lInput.value),
Number(wInput.value),
Number(hInput.value)
];
allpointsArr.sort(function(a, b){return a - b});
points.push(allpointsArr)
console.log(points);
});
in javascript sort is designed for to sort string values arrays not number array.
improve your sorting here try this:
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});