Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,861 members, 7,802,768 topics. Date: Friday, 19 April 2024 at 09:09 PM

Help, Javascript Problem, (pictures Attached) (fixed) - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help, Javascript Problem, (pictures Attached) (fixed) (741 Views)

HELP Javascript Devs! How Do I Re-write This If---else--if Statements To Run? / Javascript Problem / Help Me With This Javascript Problem (2) (3) (4)

(1) (Reply) (Go Down)

Help, Javascript Problem, (pictures Attached) (fixed) by Nobody: 8:31pm On Sep 11, 2021
Please Help me find out why this simple interest program is not working well,
It will work and calculate the simple interest only for it to display the answer(simple interest) for just a milliseconds or thereabout

Picture below

Modified: I just had to use the button tag not the input tag submit attribute because the submit attribute would refresh the page when clicked

Last two pictures show the working code

Re: Help, Javascript Problem, (pictures Attached) (fixed) by EasternActivist: 9:16pm On Sep 11, 2021
Check your form input type...
Re: Help, Javascript Problem, (pictures Attached) (fixed) by Nobody: 9:58pm On Sep 11, 2021
EasternActivist:
Check your form input type...
It's text
Re: Help, Javascript Problem, (pictures Attached) (fixed) by Brightale(f): 10:18pm On Sep 11, 2021
Re: Help, Javascript Problem, (pictures Attached) (fixed) by Brightale(f): 10:21pm On Sep 11, 2021
Think4Myself:

It's text
Since you want to move into creating apps, I will advise you to test your code using real JavaScript environments like vscode etc. The browser you are using is probably seeing your answer as those annoying pop-ups, which you probably must have deactivated yourself.
Re: Help, Javascript Problem, (pictures Attached) (fixed) by TheTraveler: 10:50pm On Sep 11, 2021
Brightale:
Since you want to move into creating apps, I will advise you to test your code using real JavaScript environments like vscode etc. The browser you are using is probably seeing your answer as those annoying pop-ups, which you probably must have deactivated yourself.
He's already in vscode, or you mean he should install node?
Re: Help, Javascript Problem, (pictures Attached) (fixed) by Capslock01: 11:15pm On Sep 11, 2021
Think4Myself:
Please Help me find out why this simple interest program is not working well,
It will work and calculate the simple interest only for it to display the answer(simple interest) for just a milliseconds or thereabout

Picture below

The issue you're having is that JavaScript script return input as a string.

You can check this with

console.log(typeof principal);
console.log(typeof rate);
console.log(typeof time);

You'll see it'll return "string"

So, to fix your issue.

Add "Number" to your declaration, it's case sensitive.

Like

var principal = Number(document. getElementById('p').value);

Do that for other declaration. It should work.
Re: Help, Javascript Problem, (pictures Attached) (fixed) by leumas91: 12:16am On Sep 12, 2021
Think4Myself:
Please Help me find out why this simple interest program is not working well,
It will work and calculate the simple interest only for it to display the answer(simple interest) for just a milliseconds or thereabout

Picture below
You need to prevent default for the submit event on the form. Also don't forget to parse input values as int.
Re: Help, Javascript Problem, (pictures Attached) (fixed) by elvisimeh(m): 8:07am On Sep 12, 2021
Prevent default will work or you can change input type='submit' to type='button'
Re: Help, Javascript Problem, (pictures Attached) (fixed) by 4dor: 9:13am On Sep 12, 2021
Think4Myself:
Please Help me find out why this simple interest program is not working well,
It will work and calculate the simple interest only for it to display the answer(simple interest) for just a milliseconds or thereabout

Picture below

1. You need to change your browser to Chrome and open your developer tools.
2. You need to console.log your values to see if it's getting anything from the input.
3. Please give your id more descriptive names.
4. I think you're missing an event listener and handler.
Re: Help, Javascript Problem, (pictures Attached) (fixed) by Nobody: 6:53pm On Sep 12, 2021
leumas91:

You need to prevent default for the submit event on the form. Also don't forget to parse input values as int.
I now understand what you mean and why my code wasn't working well
It's because sthe ubmit attribute refreshes the page when clicked hen why the simple interest was displaying for a millisecond

1 Like

Re: Help, Javascript Problem, (pictures Attached) (fixed) by Nobody: 6:53pm On Sep 12, 2021
elvisimeh:
Prevent default will work or you can change input type='submit' to type='button'
I changed it and it worked, thanks

(1) (Reply)

Php List Upgrading And Configuration / Solomon201 Pls Read This / I Need C++ Compiler

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.