+ 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!

10th Apr 2017, 11:21 PM
Zac Parnell
Zac Parnell - avatar
23 Answers
+ 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 šŸ˜€
11th Apr 2017, 12:14 AM
Jafca
Jafca - avatar
+ 14
Watch out! You have totalQ and Totalq. They're different Also it's var change = doc... not var change; doc...
11th Apr 2017, 12:15 AM
Jafca
Jafca - avatar
+ 14
Yup, it is šŸ˜„ Try this: totalQ = Math.trunc(change/q); Same for totalP etc... and then remove the Math.trunc lines
11th Apr 2017, 1:02 AM
Jafca
Jafca - avatar
+ 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
10th Apr 2017, 11:16 PM
Jafca
Jafca - avatar
+ 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?
11th Apr 2017, 12:37 AM
Jafca
Jafca - avatar
+ 13
Thanks @Rrestoring faith šŸ‘ šŸ™
11th Apr 2017, 12:42 AM
Jafca
Jafca - avatar
+ 13
@Zac Parnell If you don't mind, I'm gonna make my own version of your code ā˜ŗ
12th Apr 2017, 2:16 AM
Jafca
Jafca - avatar
+ 12
What exactly is the problem that you are having?
10th Apr 2017, 10:54 PM
Jafca
Jafca - avatar
+ 12
var change = document.getElementById("change").value;
10th Apr 2017, 11:15 PM
Jafca
Jafca - avatar
+ 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
11th Apr 2017, 1:13 AM
Jafca
Jafca - avatar
+ 12
@Rrestoring faith Good catch In my defense, I didn't touch them lines šŸ˜€
11th Apr 2017, 1:38 AM
Jafca
Jafca - avatar
+ 11
Here it is, as promised: (feel free to modify it, but please credit me) https://code.sololearn.com/WhYrW68ZDehV/?ref=app
14th Apr 2017, 8:29 PM
Jafca
Jafca - avatar
+ 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..." );
10th Apr 2017, 11:25 PM
Rrestoring faith
Rrestoring faith - avatar
+ 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.
11th Apr 2017, 12:41 AM
Rrestoring faith
Rrestoring faith - avatar
+ 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.
11th Apr 2017, 1:00 AM
Rrestoring faith
Rrestoring faith - avatar
+ 8
@Jafca @Zac note* you accidentally wrote: "remainingCents = ..." instead of remainingChange = ... line: 15 This caused that line to be ignored.
11th Apr 2017, 1:37 AM
Rrestoring faith
Rrestoring faith - avatar
+ 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).
11th Apr 2017, 12:16 AM
Rrestoring faith
Rrestoring faith - avatar
+ 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?
11th Apr 2017, 12:46 AM
Zac Parnell
Zac Parnell - avatar
+ 2
sweet! I'm getting numbers now! Is my trunc fuction wrong or misplaced because I'm getting decimals when I run anumber through
11th Apr 2017, 1:00 AM
Zac Parnell
Zac Parnell - avatar
+ 2
@jafca and I do this in the var line?
11th Apr 2017, 1:08 AM
Zac Parnell
Zac Parnell - avatar