Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
I'v got 3 input values and im tringing to sort them by size and add with js

I've got a form with 3 input values and I'm trying to sort them from smallest to highest and then do this.

  1. Add together the 2 shortest sides: 40 + 50 = 90.
  2. Multiply this number by 2: 90 x 2 = 180.
  3. Add the longest length to this number: 180 + 55 = 235.

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);

          });
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

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}); 

you can refer from this link

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!