1

I've trying to add an if condition.

Condition one If the down payment is below 35% of the price then "Your downpayment should be above 35% of the price"

Condition two If the down payment is above 100% of the price then "your downpayment should be below 100% of the price"

condition three if the downpayment is between 35%-100% then do the math.

The answer isn't working in my below coding, where did I go wrong?

 if(parseInt(downPayment) < 0.35*parseInt(vprice) || parseInt(downPayment) = parseInt(vprice)) {
  
  document.getElementById("month").innerHTML = "Your downpayment should be minimum 35% of the price - "  +" (↑"+ nf.format((Math.round(vprice*0.35))) +")";
  
  }
  
  
  else if(parseInt(downPayment) > parseInt(vprice)) {
  
  document.getElementById("month").innerHTML = "Your downpayment should be below 100% of the price - "  +" (↑"+ nf.format((Math.round(vprice*0.100))) +")";
  
  }
  
  else {
  
  document.getElementById("month").innerHTML = " 1st Year Monthly Rental: "+ nf.format((Math.round(month))) + "<br>2nd Year Monthly Rental: " +  nf.format((Math.round(month2))) + "<br>3rd Year Monthly Retal:"+  nf.format((Math.round(month3))) ;
  google.script.run.userClicked({
    vprice,
    downPayment,
    rate,
    period,
    month
  });
  //sending to HTML by ID
  document.getElementById("subL").innerHTML = "Sub Loan - "+  nf.format((Math.round(subL)));
  document.getElementById("YearBulk1").innerHTML = "1st Year End Bulk Payment - "+ nf.format((Math.round(YearBulk1)));
 document.getElementById("YearBulk2").innerHTML = "2nd Year End Bulk Payment - "+ nf.format((Math.round(YearBulk2)));
 document.getElementById("YearBulk3").innerHTML = "3rd Year End Bulk Payment - "+ nf.format((Math.round(YearBulk3)));
  
  
  }

1 Answer 1

2

Explanation:

  • You have a typo in the first if statement; it should be == instead of = but you can also simplify the expression by using <=.

  • You must put document.getElementById("month").innerHTML outside of the if conditions, otherwise the rest of the elements won't be send to the webpage.

  • I also used template literals to simplify your expression and make it more futureproof as well.


Solution:

if(parseInt(downPayment) <= 0.35*parseInt(vprice) ) {
  var resm = `Your downpayment should be minimum 35% of the price - (↑ ${nf.format((Math.round(vprice*0.35)))})`;
  }
  
  
  else if(parseInt(downPayment) > parseInt(vprice)) {
    var resm = `Your downpayment should be below 100% of the price - (↑ ${nf.format((Math.round(vprice*0.100)))})`;
  }
  
  else {
var resm = `1st Year Monthly Rental: ${nf.format((Math.round(month)))} <br>2nd Year Monthly Rental: ${nf.format((Math.round(month2)))} <br>3rd Year Monthly Retal: ${nf.format((Math.round(month3)))}`;
  }


  document.getElementById("month").innerHTML = resm;
  google.script.run.userClicked({
    vprice,
    downPayment,
    rate,
    period,
    month
  })

  //sending to HTML by ID
  document.getElementById("subL").innerHTML = "Sub Loan - "+  nf.format((Math.round(subL)));
  document.getElementById("YearBulk1").innerHTML = "1st Year End Bulk Payment - "+ nf.format((Math.round(YearBulk1)));
 document.getElementById("YearBulk2").innerHTML = "2nd Year End Bulk Payment - "+ nf.format((Math.round(YearBulk2)));
 document.getElementById("YearBulk3").innerHTML = "3rd Year End Bulk Payment - "+ nf.format((Math.round(YearBulk3)));
Sign up to request clarification or add additional context in comments.

7 Comments

How to make disappear of the //sending to HTML by ID ?
@MuhammedAadhil I am not quite sure what you mean. Could you please post a separate question for that? :) Stackoverflow does not allow follow-up questions.
I figured it out after few placement changes. Thanks :)
Anyware I'll post another question just to ensure I'm made it all perfect. Please check and let me know
@MuhammedAadhil then your code is correct. You should put it in the else statement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.