+ 2
Need help making a code that displays coin amounts
I'm supposed to make a program in javascript that will display the amount of coins that are needed to make the number inputed by a user. I have what code I have written on the playground here(https://code.sololearn.com/WWuH1Uln935H/#html). I need any and all help I can get. Thank you! Edit 1: Thanks so much for the suggestions! I implemented what I read below and now when I got to run, I get the error that my function at the top "change_program" isnt defined. How can I fix this error? This is my first javascript program so I'm sorry if there is an obvious or super simple fix I'm missing!
23 odpowiedzi
+ 14
It's better to not use document.write()
Use the paragraph that you have with an id of "demo".
Use document.getElementById("demo") and then set it's innerHTML to "Your change is " + totalQ + " quarters and... ";
Edit:
Or just do what @Rrestoring faith said 😀
+ 14
Watch out! You have totalQ and Totalq. They're different
Also it's var change = doc...
not var change; doc...
+ 14
Yup, it is 😄
Try this:
totalQ = Math.trunc(change/q);
Same for totalP etc...
and then remove the Math.trunc lines
+ 13
In the last line, replace q, d, n and p with totalQ etc.
And round down the values of totalQ etc. by truncating them
+ 13
"Watch out! You have totalQ and Totalq. They're different"
Also could you update the code that you inserted in your question so we can see your problem?
+ 13
Thanks @Rrestoring faith 👍 🙏
+ 13
@Zac Parnell If you don't mind, I'm gonna make my own version of your code ☺
+ 12
What exactly is the problem that you are having?
+ 12
var change = document.getElementById("change").value;
+ 12
@Zac
function change_program() {
var change = document.getElementById("change").value;
var q = 25; //quarters
var d = 10;//dimes
var n = 5;//nickels
var p = 1;//pennies
var totalq, totald, totaln, totalq, remainingChange;
totalq = Math.trunc(change / q);
remainingChange = change % q;
totald = Math.trunc(remainingChange / d);
remainingChange = remainingChange % d;
totaln = Math.trunc(remainingChange / n);
remainingCents = remainingChange % n;
totalp = Math.trunc(remainingChange / p);
document.write("Your change is "+totalq+" quarters, "+totald+" dimes, "+totaln+" nickles, and "+totalp+" pennies.")
}
That should do it
+ 12
@Rrestoring faith Good catch
In my defense, I didn't touch them lines 😀
+ 11
Here it is, as promised:
(feel free to modify it, but please credit me)
https://code.sololearn.com/WhYrW68ZDehV/?ref=app
+ 8
First fix:
* You're missing a semi-colon on line 3, and line 10 in the JS section.
* Document.write needs quotes cause it accepts a String, use concatenation to separate the strings and variables.
Like this: document.write("Your Change is ["+q+"] etc..." );
+ 8
@zac
Javascript is case sensitive
Your variable is named totalQ.
But in Math.trunc() you wrote Totalq. Same thing in document.write(..);
Make sure the 't' is lowercase, and the 'Q'/'N' etc.. is uppercase.
+ 8
var change;
You never set change equal to anything.
I think this is what you meant to do:
var change = document.getElementById("change").value;
(Line 3).
Once I made that fix it worked for me.
+ 8
@Jafca @Zac note* you accidentally wrote:
"remainingCents = ..."
instead of remainingChange = ...
line: 15
This caused that line to be ignored.
+ 7
@Zac You need to have it like how I wrote it in my first post.
document.write("Your change is "+totalQ+" quarters, "+totalD+" etc...");
But instead of using document.write, you could use what @Jafca said.
Quote the words, leave the variables un-quoted. Separate the words from the variables with a '+' sign (this is called concatenation).
+ 2
Okay one last question to get it up and running completely. When I click the button everything works but it gives me NaN instead of numbers, where did I mess up here?
+ 2
sweet! I'm getting numbers now! Is my trunc fuction wrong or misplaced because I'm getting decimals when I run anumber through
+ 2
@jafca and I do this in the var line?