函数总和工作不正常 [英] Function sum not working properly javascript

查看:76
本文介绍了函数总和工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加两个字段时,代码并没有总结它,而是添加了我输入的内容,继承我的代码

  function calculate_loan(){
var amountBorrowed = document.form.amountBorrowed.value;
var interest = document.form.interest.value / 100;
var payments = document.form.durationOfPayment.value;
var monthInterest = document.form.interestPerMonth.value;
var totalInt = document.form.totalinterest.value;


var monthly = amountBorrowed * interest;
var totalinterest = monthly * payments;
var totalAmount = amountBorrowed + totalinterest; $!

$ b if(!isNaN(monthly)&&
(monthly!= Number.POSITIVE_INFINITY)&&
(monthly!)= Number.NEGATIVE_INFINITY )){


document.form.total.value = round(totalAmount);
document.form.totalinterest.value =
round(monthly * payments);
document.form.interestPerMonth.value =
round(每月);
}

else {
document.form.amountOfPayement.value =;
document.form.total.value =;
document.form.totalinterest.value =;



函数round(x){
return Math.round(x * 100)/ 100;
}

例如每月利息是:720总利息:2160借入金额:12000结果是120002160.我希望得到的结果是14,160请帮忙谢谢

>

  var amountBorrowed = Number(document.form.amountBorrowed.value); 
var interest = Number(document.form.interest.value)/ 100;
var payments = Number(document.form.durationOfPayment.value);
var monthInterest = Number(document.form.interestPerMonth.value);
var totalInt = Number(document.form.totalinterest.value);


When i add two fields, the code doesn't sum it but instead, it adds both what I inputted, heres my code

function calculate_loan() {
  var amountBorrowed = document.form.amountBorrowed.value;
  var interest = document.form.interest.value / 100;
  var payments = document.form.durationOfPayment.value;
  var monthInterest = document.form.interestPerMonth.value;
  var totalInt = document.form.totalinterest.value;


 var monthly = amountBorrowed * interest;
 var totalinterest = monthly * payments;
 var totalAmount = amountBorrowed + totalinterest;


  if (!isNaN(monthly) &&
    (monthly != Number.POSITIVE_INFINITY) &&
    (monthly != Number.NEGATIVE_INFINITY)) {


    document.form.total.value = round(totalAmount);
    document.form.totalinterest.value =
      round(monthly * payments);
          document.form.interestPerMonth.value =
      round(monthly);
  }

  else {
    document.form.amountOfPayement.value = "";
    document.form.total.value = "";
    document.form.totalinterest.value = "";
  }
}

function round(x) {
  return Math.round(x * 100) / 100;
}

for example Interest per month is : 720 Total interest: 2160 Amount Borrowed: 12000 the result is 120002160. I want the result to be 14,160 please help thanks

解决方案

You should parse the values as numbers

var amountBorrowed = Number(document.form.amountBorrowed.value);
var interest = Number(document.form.interest.value) / 100;
var payments = Number(document.form.durationOfPayment.value);
var monthInterest = Number(document.form.interestPerMonth.value);
var totalInt = Number(document.form.totalinterest.value);

这篇关于函数总和工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆