Modify_inc Posted May 6, 2013 Share Posted May 6, 2013 Can you tell me why this line works, but then why does it put undefined before the answer though?It's like it's trying to concatenate the variable instead of just displaying the stored value for the variable.I say that because I initialized the m variable to zero, and it displayed the Morning total as 015 (appending the zero to the total)if it helps, I am using m += cards to achieve this. Also tried m = m + cards with same results.document.write("Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e);Thanks againMike Link to comment Share on other sites More sharing options...
davej Posted May 6, 2013 Share Posted May 6, 2013 (edited) Before continuing it would probably be a good idea to convert this to; <!DOCTYPE html><html lang="en"><head><script>window.onload = function(){var out = document.getElementById('out');var m = 1;var n = 2;var e = 3;var cards = 4;m += cards;out.innerHTML = "Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e;}</script></head><body><div id="out"></div></body></html> Edited May 6, 2013 by davej Link to comment Share on other sites More sharing options...
Modify_inc Posted May 6, 2013 Author Share Posted May 6, 2013 (edited) Before continuing it would probably be a good idea to convert this to; <!DOCTYPE html><html lang="en"><head><script>window.onload = function(){var out = document.getElementById('out');var m = 1;var n = 2;var e = 3;var cards = 4;m += cards;out.innerHTML = "Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e;}</script></head><body><div id="out"></div></body></html> Thanks for the suggestion, but I would really like to know why the format I used did not perform the calculations as intended. To add to a variable, do you not type m=m+5 or m+=5. So that whatever is already assigned to the m variable, 5 is now added to it? So if originally, m=10, m would now equal 15. What am I doing wrong? This is all of my code: (I'm learning!)// Declare variables and constantsvar cat; // categoryvar cards ; // # of cards per categoryvar type; // temp value for category typevar m = 0; // morning variablevar n = 0; // noon variablevar e = 0; // evening variablevar final; // temp variable to calculate all totalsvar BR = "<br />"; // HTML line breakvar ES = ""; // literal empty stringwhile (type != 4) { // Reinitalize variables cat = "a";cards = "a"; // Welcome the user document.write("Welcome to Mack's Coffee Cove!"+ BR); // Check input while (cat != "morning" && cat != "noon" && cat != "evening" && cat != "t") { cat = prompt("Enter the card Category (morning, noon, evening), Press t to total all 3 categories: ",ES); } // Determine type if (cat == "morning") { type = 1; } else if (cat == "noon") { type = 2; } else if (cat == "evening") { type = 3; } else { type = 4 } if (cat != "t") { // Check input while (isNaN(cards)) { cards = prompt("Enter the number of cards collected for the " + cat + " batch: ",ES); if (type == 1) { m += cards; document.write("You entered " + cards + " survey cards for the morning." + BR); } else if (type == 2) { n += cards; document.write("You entered " + cards + " survey cards for noon." + BR); } else if (type == 3) { e += cards; document.write("You entered " + cards + " survey cards for the evening." + BR); } } }}// Sum and display totals for all categoriesfinal = m + n + e;document.write("Totals for all categories: " + final + BR);document.write("Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e + BR);// Thank the clientdocument.write("Thank you!" + BR); Edited May 6, 2013 by Modify_inc Link to comment Share on other sites More sharing options...
justsomeguy Posted May 6, 2013 Share Posted May 6, 2013 The + operator is also used to join strings. The prompt function returns a string, not a number. When you use + with a string and number it casts the number as a string and then concatenates them, it doesn't add numbers. If you want to add numbers you need to convert the return value from prompt into a number. You can use parseInt or parseFloat to do that. Link to comment Share on other sites More sharing options...
Modify_inc Posted May 6, 2013 Author Share Posted May 6, 2013 (edited) Thank you, that worked!! I had forgot about the parseInt command. Edited May 6, 2013 by Modify_inc Link to comment Share on other sites More sharing options...
davej Posted May 7, 2013 Share Posted May 7, 2013 (edited) This is actually pretty horrible to use... <!DOCTYPE html><html lang="en"><head><script>window.onload = function(){// Declare variables and constantsvar out = document.getElementById('out');//output div elementvar cat; // categoryvar cards ; // # of cards per categoryvar type; // temp value for category typevar m = 0; // morning variablevar n = 0; // noon variablevar e = 0; // evening variablevar final; // temp variable to calculate all totalsvar BR = "<br/>"; // HTML line breakvar ES = ""; // literal empty stringwhile (type != 4) { // Reinitalize variables cat = "a";cards = "a"; // Welcome the user //document.write("Welcome to Mack's Coffee Cove!"+ BR); out.innerHTML = "Welcome to Mack's Coffee Cove!"+ BR; // Check input while (cat != "morning" && cat != "noon" && cat != "evening" && cat != "t") { cat = prompt("Enter the card Category (morning, noon, evening), Press t to total all 3 categories: ",ES); } // Determine type if (cat == "morning") { type = 1; } else if (cat == "noon") { type = 2; } else if (cat == "evening") { type = 3; } else { type = 4 } if (cat != "t") { // Check input while (isNaN(cards)) { cards = Number(prompt("Enter the number of cards collected for the " + cat + " batch: ",ES)); if (type == 1) { m += cards; //document.write("You entered " + cards + " survey cards for the morning." + BR); out.innerHTML += "You entered " + cards + " survey cards for the morning." + BR; } else if (type == 2) { n += cards; //document.write("You entered " + cards + " survey cards for noon." + BR); out.innerHTML += "You entered " + cards + " survey cards for noon." + BR; } else if (type == 3) { e += cards; //document.write("You entered " + cards + " survey cards for the evening." + BR); out.innerHTML += "You entered " + cards + " survey cards for noon." + BR; } } }}// Sum and display totals for all categoriesfinal = m + n + e;//document.write("Totals for all categories: " + final + BR);out.innerHTML += "Totals for all categories: " + final + BR;//document.write("Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e + BR);out.innerHTML += "Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e + BR;// Thank the client//document.write("Thank you!" + BR);out.innerHTML += "Thank you!" + BR;}</script></head><body><div id="out"></div></body></html> Just from the standpoint of ease-of-use I would try re-writing it in a form more like this... <!DOCTYPE html><html lang="en"><head><style>#morn,#noon,#eve {width: 40px;}h2{font-family: "Roman", Impact, 'Comic Sans';color: blue;}</style><script>window.onload = function(){// Declare variables and constantsvar tot = document.getElementById('tot');var morn = document.getElementById('morn');var noon = document.getElementById('noon');var eve = document.getElementById('eve');tot.innerHTML = '';morn.value = '0';noon.value = '0';eve.value = '0';btn.onclick = calculate;function calculate(){var sum = Number(morn.value) + Number(noon.value) + Number(eve.value);if (isNaN(sum)){tot.innerHTML = "Error";}else{tot.innerHTML = sum;}}//end calculate}//end of onload</script></head><body><h2>Welcome to Mack's Coffee Cove!</h2><br/><table><tr><td>Morning cards:</td><td><input type="text" id="morn"/></td></tr><tr><td>Noon cards:</td><td><input type="text" id="noon"/></td></tr><tr><td>Evening cards:</td><td><input type="text" id="eve"/></td></tr><tr><td>Total cards:</td><td><span id="tot"></span><td></td></tr></table><br/><input type="button" id="btn" value="Add Total"/></body></html> Edited May 7, 2013 by davej Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now