/    Sign up×
Community /Pin to ProfileBookmark

Calculations in Javascript don’t work

Can anyone tell me why the result of the javascript doesn’t output into the h2 tags. This is my second day of coding and any help would be much appreciated….thanks

HTML
<!DOCTYPE html>
<html>
<head>
<title>Goal Calculator</title>
<link rel=”stylesheet” href=”styles.css”>
<script>
function calculate() {
var sbal = document.getElementById(‘sbal’).value;
var goal = document.getElementById(‘goal’).value;
var exp = document.getElementById(‘exp’).value;
var accben = document.getElementById(‘accben’).value;
var main = ((goal – (sbal – accben)) / 12 );
var result = (main + exp).tofixed(2);
payment = payment.toString().replace(/B(?=(d{3})+(?!d))/g, “,”);
document.getElementById(‘payment’).innerHTML = “Monthly Payment = $”+payment;
}

</script>

</head>
<body>
<div class=”wrapper”>
<div class=”contact-form”>
<div class=”input-fields” id=”calculate-form”>
<div class=”drop”>
<label>Time Period</label>
<select id = “myList”>
<option value = “1”>Week</option>
<option value = “2”>Fortnight</option>
<option value = “3”>Year</option>
</select>
</div>
<input id=”sbal” class=”input” type=”number” placeholder=”Starting Balance”>
<input id=”goal” class=”input” type=”number” placeholder=”Overall Goal”>
<input id=”exp” class=”input” type=”number” placeholder=”Expenditure”>
<input id=”accben” class=”input” type=”number” placeholder=”Account Benchmark”>
<h2 id=”payment”></h2>
</div>
</div>
</div>
</div>
<div>
<button class=”btn” id=”footer” onclick=”calculate()”>Submit</button>
</div>
</body>
</html>

CSS
*{
margin: 0;
padding: 0;
box-sizing: border-box;
outline: none;
font-family: cursive, sans-serif;
}

body{
background: url(‘flex.png’) no-repeat top center;
background-size: cover;
height: 100vh;
}

.logo img{
position: relative;
left: 25px;
padding: 0px;
float: left;
width: 150px;
height: auto;
margin-top:10px;
margin-left: 10px;

}

.wrapper{
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 100%;
padding: 0 20px;
}

.drop{
position: relative;
left:15px;
}

.myList:hover {
background-color: rgba(0,0,0,0.1);
}

select{
background-color: transparent;
border: transparent;
position: relative;
left: 40px;
color: #fff;
cursor: pointer;
}

select:active {
background-color: transparent;
}

label {
position: relative;
left: 15px;
color: #fff;
}

.contact-form{
display: flex;
max-width: 450px;
margin: 0 auto;
background: rgba(0,0,0,0.8);
padding: 30px;
border: 5px solid #fff;
box-shadow: 0 0 10px rgba(0,0,0,0.3);
}

.input-fields{
position: relative;
left: 115px;
display: flex;
flex-direction: column;
margin-right: 4%;
position: relative;
}

.input-fields,
.msg{
width: 44%;
}

.input-fields .input,
textarea{
margin: 10px 0;
background: transparent;
border: 0px;
border-bottom: 2px solid #fff;
padding: 10px;
color: #fff;
width: 100%;
}

h2 {
text-align: center;
position: relative;
top: 15px;
color: #fff;
}

#footer {
position: absolute;
bottom: 0px;
width: 100%;
}

.btn {
background: #000;
text-align: center;
padding: 15px;
border-radius: 5px;
border-style: none;
color: #fff;
cursor: pointer;
text-transform: uppercase;
}

a {
text-decoration: none;
color: #fff;
}

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@mikenath223Dec 27.2019 — Try changing this:

document.getElementById('payment').innerHTML = "Monthly Payment = $"+payment;

To this: document.getElementById('payment').innerText = "Monthly Payment = $"+payment;

Let me know if that resolves it.
Copy linkTweet thisAlerts:
@KeverDec 27.2019 — All the values you get from input elements are strings. When you do substraction, division or multiplication on them they are treated as numbers, so 'main' is a number. When you add main (number) to exp (string) the result is a string, which doesn't have the toFixed() (uppercase F) method. The line payment = payment... doesn't make sense either.
function calculate() {
var sbal = document.getElementById('sbal').value;
var goal = document.getElementById('goal').value;
var exp = document.getElementById('exp').value;
var accben = document.getElementById('accben').value;
var main = ((goal - (sbal - accben)) / 12 );
var result = (main + Number(exp)).toFixed(2);
var payment = result.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
document.getElementById('payment').innerHTML = "Monthly Payment = $"+payment;
};
Copy linkTweet thisAlerts:
@mikenath223Dec 27.2019 — > @T1Alpha#1612184 var sbal = document.getElementById('sbal').value;

> var goal = document.getElementById('goal').value;

> var exp = document.getElementById('exp').value;

> var accben = document.getElementById('accben').value;


Try changing the following variables to numbers by doing this . Let me give you an example:

sbal = sbal.toNumber()

Do the same for the remaining 3 variables also.
Copy linkTweet thisAlerts:
@SempervivumDec 27.2019 — ["HELP ME PLEAASEEE!!!!","Calculations in Javascript don't work"]
Copy linkTweet thisAlerts:
@SempervivumDec 27.2019 — @T1Alpha#1612184 Hi T1Alpha, please use meaningful titles for your posts. I've changed the title here.
Copy linkTweet thisAlerts:
@MiqoDec 28.2019 — Opened LifeInSYS digital marketplace where you can find free and premium WordPress themes, html templates, PSD files and much more... Also, you can become a author and sell your products and earn money, first 100 registered authors will be fees free and get 100% earning from sale.

Marketplace link: http://lifeinsys.com
×

Success!

Help @T1Alpha spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...