Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:06:29+00:00 2026-06-12T23:06:29+00:00

I met trouble with a script I have done, this script should allocate an

  • 0

I met trouble with a script I have done,
this script should allocate an amount to different fields.

In fact here is the script, I will give you after the jsfiddle.

 <script type="text/javascript">
function getItems()
 {
 var items = new Array();
 var itemCount = document.getElementsByClassName("items");

 for(var i = 0; i < itemCount.length; i++)
 {
     items[i] = document.getElementById("p"+(i+1)).value;
 }

 return items;
 }

 function setItems(items,payAmt)
 {
   //document.getElementById("troppercu").value = payAmt;
   for(var i = 0; i < items.length; i++)
   {
      document.getElementById("p" + (i+1)).value = items[i];
   }
 }

 function itemSum(items)
 {
var sum = 0;

for(var i=0; i < items.length; i++)
{
    sum = items[i] + sum;
}

return sum;
 }

 function payment(inputElm)
 {
 var items = getItems();
 var payAmt = document.getElementById("montantacompte").value;
 var i = 0;
 var sum = itemSum(items);

 while(payAmt != 0 && sum != 0)
 {
     var temp = items[i] - payAmt;
     if(temp > 0)
     {
         items[i] = Math.round((temp)*100)/100;
         break;
     }
     else if(temp < 0)
     {
         items[i] = 0;
         payAmt = temp*-1;
      }
     i++;
     sum = itemSum(items);
 }

  setItems(items, payAmt);


 }
</script>

So I have an input in which I can write an amount, this amount will be allocated on the different fields.

Actually, the trouble is that when I put an amount that is the same as the principal amount, it freezes the screen. If I write the same amount + 1 cent, it does not freeze.

All information on the fields come from my database. that is written with PHP and MySQL.

http://jsfiddle.net/ywAU3/

The trouble comes when you write the same amount as in the principal so for that case it will freeze the screen if you write on the yellow box input 3654.58 I really do not understand why, in fact, it should not, because if I write 3654.59 all works, If I write more, it works also.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:06:31+00:00Added an answer on June 12, 2026 at 11:06 pm

    The problem is in while cycle,

    you have

    if(temp > 0)
    

    and

    else if(temp < 0)
    

    But what if

    temp=0
    

    That causes your while cycle to work forever… I included temp = 0 case and then code worked fine:
    http://jsfiddle.net/ywAU3/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I met a trouble with a script I have done. Here is the entire
I met a trouble with a form. In fact I have a php dynbamic
I met a problem like this. class A have a list of class B.
I have met a problem with virtualbox, I hope someone could help me here,
I met strange problem. I have never work with Devise gem before. So, i
Has anyone met this error? Right now i am trying to do switch from
I'm having trouble inserting a block of text when a certain condition is met
I met some trouble in php json decode number. $json = '[{num:123456789011121314},{num:1516171819202122232425}]'; $number =
I met some basic problem in jquery, but I can't figure it out. Here
Many developers I have met suggest it's best practice to go with simple loops

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.