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

237
Views
¿Cómo SUMAR el resto en Javascript?

Soy un principiante de Javascript y estoy tratando de resolver un problema en Javascript https://onlinejudge.org/external/116/11689.pdf ¿Cómo puedo sumar el resto en el ciclo for?

Aquí está el código al que llegué hasta ahora...

 function pant(e, f, c) { let t=2; for(let i=0;i<t;i++){ let pant=e+f; let buy=pant/c; let buy2=buy/c; //total bottles let total=buy+buy2; let rem=total%c; console.log(rem); return total; }}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esta respuesta de uingtea es muy precisa y usa el ciclo while . Lo mismo se puede lograr usando la recursividad como se muestra a continuación.

Fragmento de código

 // given "curr" number of caps and "cost" of each soda // return number of sodas that can be purchased const recurseCalc = (curr, cost) => { // number of sodas is "curr" / "cost" (rounded to lower integer) const numOfSodas = Math.floor(curr/cost); // remaining caps is "curr" less caps paid to obtain "numOfSodas" const remainingCaps = curr - (numOfSodas * cost); // console-log to help understand each recursion console.log( 'curr: ', curr, 'cost: ', cost, 'num: ', numOfSodas, 'rem: ', remainingCaps, 'curr >= cost: ', curr >= cost ? 'yes' : 'no' ); // if "curr" caps is more than "cost" of a soda // then, recursively calculate number of sodas // else, zero sodas can be purchased (since "curr" is lower than "cost") return ( curr >= cost ? numOfSodas + recurseCalc( numOfSodas + remainingCaps, cost ) : 0 ); }; // use recursively calculate number of sodas // for total caps (ie, "e" + "f") const howManySodas = (e, f, c) => recurseCalc(e + f, c); console.log('result\t---->| e: 9, f: 0, c: 3, sodas: ', howManySodas(9, 0, 3), ' |<---'); console.log('result\t---->| e: 5, f: 5, c: 2, sodas: ', howManySodas(5, 5, 2), ' |<---'); console.log('result\t---->| e: 7, f: 3, c: 3, sodas: ', howManySodas(7, 3, 3), ' |<---');
 .as-console-wrapper { max-height: 100% !important; top: 0 }

Explicación

Comentarios en línea agregados en el fragmento anterior.

about 4 years ago · Santiago Trujillo Report

0

necesitas while loop y parseInt

 function pant(e, f, c) { let testNumber = `${e} ${f} ${c}`, result = 0 e += f; while (e >= c) { result += parseInt(e / c) e = e % c + parseInt(e / c) } console.log(`result for ${testNumber}: ${result}`) } pant(9, 0, 3) pant(5, 5, 2)

about 4 years ago · Santiago Trujillo 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!