I got undefined in the first iteration any comments is helpful to me thanks in advance
for (i = 0; i <= nummonth; i++) {
var res = compound * (Math.pow(interest, i));
res = res.toFixed(0);
var tableHTML;
var interestgained = (+res * interest)-res;
interestgained = interestgained.toFixed(0);
var powint = compound * (Math.pow(interest, i));
powint = powint.toFixed(0);
var tr = powint - compound;
var ointerest = (interest - 1)*100;
ointerest = ointerest.toFixed(0);
tableHTML = tableHTML + "<tr><td>Compound: " + i + "</td><td> " + "$"+res + "</td><td> " + ointerest + "%"+"</td><td> " + "$"+interestgained + "</td><td>"+ "$"+powint +"</td></tr>";
}
I'm trying to work out the interest of values that're inputed.
I ask for the principle rate, the rate in percentage and the number of years.
I want to figure out their simpleInterest and compundInterest.
I want it to display the result value for each year in a table.
I have a table that's getting the number of years when I type in the amount, for example 15. When I type in 15, it shows 15 rows but they're all the same.
I need it to work out the value for each year on a each row.
My current code is only showing 15 years as the same result.
My code :
<div class="container">
<div id="content">
<h2 class="interestOutput">Table for $<span id="interestPrinciple">Loading...</span> at <span id="interestRate">Loading...</span>%
</h2>
<table>
<tr>
<th>Year</th>
<th>SimpleInterest</th>
<th>CompoundInterest</th>
</tr>
<div id="tableResult">
</div>
</table>
</div>
<script type="text/javascript">
if (document.readyState = "complete") {
var principle = localStorage.getItem("principle");
var rate = localStorage.getItem("rate");
var years = localStorage.getItem("years");
var simple_interest = principle * (rate / 100) * years;
var compound_interest = principle * (1 + rate/100);
/*
var list = computeSchedule(p, rate, 12, y, monthlyPayment);
var tables = "";
for (var i = 0; i < list.length; i++) {
tables += "<tr>" +
"<td>" + list[i][0] + "</td>" +
"<td>" + list[i][1] + "</td>" +
"<td>" + list[i][2] + "</td>" +
"<td>" + list[i][3] + "</td>" +
"<td>" + list[i][4] + "</td>" +
"</tr>";
}
document.getElementById("demo").innerHTML = '<table>' + tables + '</table>';
*/
var tables = "";
for (var i = 0; i < years; i++) {
tables +=
"<tr>" +
"<td>" + years + "</td>" +
"<td>" + simple_interest + "</td>" +
"<td>" + compound_interest + "</td>"+
"</tr>";
}
document.getElementById("tableResult").innerHTML = '<table>' + tables + '</table>';
document.getElementById("interestPrinciple").innerHTML = principle;
document.getElementById("interestRate").innerHTML = rate;
//document.getElementById("gradeOutput").innerHTML = years;
}
</script>
I have now sorted the simple interest column but the compound column is not correct.
I was given this information : The formula to compute simple interest is interest = principal * (rate/100) * years. Just add the principal to the interest to generate the amount to display. The formula to compute compound interest is FinalAmount = principal * (1 + rate/100)Years. Notice that you do not need to add the principal in this case. Your program must read the principal amount, rate (%), and the maximum number of years (see Web Site Snapshots/Video below). Use the following messages to read the appropriate values:
My updated code is :
<div class="container">
<div id="content">
<h2 class="interestOutput">Table for $<span id="interestPrinciple">Loading...</span> at <span id="interestRate">Loading...</span>%
</h2>
<div id="tableResult"></div>
</div>
</div>
<script type="text/javascript">
if (document.readyState = "complete") {
var principle = localStorage.getItem("principle");
var rate = localStorage.getItem("rate");
var years = localStorage.getItem("years");
var tables = "";
for (var i = 0; i < years; i++) {
var simple_interest = principle * (rate / 100) * i;
var compound_interest = principle * (1 + (rate/100));
var final_simple = compound_interest + simple_interest;
var add_extra = 1 + (rate/100);
var final_compound = final_simple + add_extra;
tables +=
"<tr>" +
"<td>" + (i + 1) + "</td>" +
"<td>$" + final_simple + "</td>" +
"<td>$" + final_compound + "</td>"+
"</tr>";
}
document.getElementById("tableResult").innerHTML = '<table><tr><th>Year</th><th>SimpleInterest</th><th>CompoundInterest</th></tr>' + tables + '</table>';
document.getElementById("interestPrinciple").innerHTML = principle;
document.getElementById("interestRate").innerHTML = rate;
}
</script>
From what I can see, nothing about your iteration over the years is calling the problem solving functions. Your variables are declared once with the data available as is, and you're literally looping over the same data years amount of times.
Try something like this.
var tables, principle, years, rate, add_extra, times_compunded_per_tear, compound_rate, i;
principle = 10000;
years = 6;
rate = 5.6;
tables = '';
times_compounded_per_year = 12
compound_rate = 1 / times_compounded_per_year;
function simple_interest(currentYear) {
return principle * (rate / 100) * currentYear + principle;
}
function compound_interest(currentYear) {
return principle * (Math.pow(((1 + rate/100) / compound_rate), (compound_rate * currentYear)));
}
for (i = 0; i < years + 1; i++) {
tables +=
"<tr>" +
"<td>Year: "+ i +"</td>" +
"<td>Simple Interest "+ simple_interest(i).toFixed(2) + "</td>" +
"<td>Compound Interest "+ compound_interest(i).toFixed(2) + "</td>" +
"</tr>";
}
document.getElementById("tableResult").innerHTML = '<table>' + tables + '</table>';
document.getElementById("interestPrinciple").innerHTML = "Principle: "+principle;
document.getElementById("interestRate").innerHTML = "Interest Rate: "+rate;
//document.getElementById("gradeOutput").innerHTML = years;
<ul>
<li id="interestPrinciple"></li>
<li id="interestRate"></li>
</ul>
<div id="tableResult"></div>
Doing the same thing again and again will always give you the same results. In order for the results to change you need to use the index somewhere in your code.
Also why all the comments, clean your code, ask a precise question and you will get a precise answer.
You need to calculate the interest inside of the for loop otherwise you are just recycling the same variable each time and never updating it.
for (var i = 0; i < years; i++) {
var simple_interest = principle * (rate / 100) * i;
var compound_interest = principle * (1 + rate/100);
tables +=
"<tr>" +
"<td>" + (i + 1) + "</td>" +
"<td>" + simple_interest + "</td>" +
"<td>" + compound_interest + "</td>"+
"</tr>";
}
Also the compound_interest will need to use the index as well.
I want to create a button that produces a 3x3 HTML table meant for a card game, where each cell corresponds to one single image. For the time being, I'm mainly focusing on filling individual cells with a card number taken from an array element, using a step-by-step rationale. This is what the final result should look like in HTML, derived from the produced script;
<table style="width:100%">
<tr>
<td><img src="1.png"></td>
<td><img src="2.png"></td>
<td><img src="3.png"></td>
</tr>
<tr>
<td><img src="4.png"></td>
<td><img src="5.png"></td>
<td><img src="6.png"></td>
</tr>
<tr>
<td><img src="7.png"></td>
<td><img src="8.png"></td>
<td><img src="9.png"></td>
</tr>
</table>
This is what I'm currently working at, although with not much success
<!DOCTYPE html>
<html>
<body>
<table id="1">
</table>
<button onclick="createTable()">Create</button>
<script>
function createTable() {
var deck = [1,2,3,4,5,6,7,8,9];
function shuffle(o) { //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(deck);
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>" +
;
}
</script>
</body>
</html>
Note
The 9-card deck is shuffled using a pretty basic and self-explanatory function.
Edit. My final version, which is returning broken image.
<!DOCTYPE html>
<html>
<body>
<table id="1">
</table>
<button onclick="createTable()">Create</button>
<script>
function createTable() {
var deck = [1,2,3,4,5,6,7,8,9];
function shuffle(o) {
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(deck);
document.getElementById("1").innerHTML = "<tr><td><img src=" + deck[0] + ".png'></td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>"
;
}
</script>
</body>
</html>
After you fix your code by removing the extra + at the last line where you build your html (tr's and td's), you will simply need just to replace
"<td>" + deck[0] + "</td>"
by
"<td><img src='" + deck[0] + ".png'></td>"
... and so on for other indexes like deck[1], deck[2] etc.
You have got an extra "+"
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>";
Should work better.
Your shuffle() is pretty convoluted. Let me advise you to write less exotic code. Making your code more readable will help to find potential errors. Indeed, the problem is not so hard to locate actually: https://stackoverflow.com/a/34471591/1636522 :-)
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write(JSON.stringify(shuffle(deck), 0, 1));
function shuffle (anArray) {
var i, j, x, l = anArray.length;
for (i = 0; i < l; i++) {
j = rdmInt(l);
x = anArray[i];
anArray[i] = anArray[j];
anArray[j] = x;
}
return anArray;
}
function rdmInt (max) {
return Math.floor(Math.random() * max);
}
The same remark applies to your string concatenation:
document.getElementById("1").innerHTML = ""
+ "<tr>"
+ "<td><img src=" + deck[0] + ".png'></td>"
+ "<td>" + deck[1] + "</td>"
+ "<td>" + deck[2] + "</td>"
+ "</tr>"
+ "<tr>"
+ "<td>" + deck[3] + "</td>"
+ "<td>" + deck[4] + "</td>"
+ "<td>" + deck[5] + "</td>"
+ "</tr>"
+ "<tr>"
+ "<td>" + deck[6] + "</td>"
+ "<td>" + deck[7] + "</td>"
+ "<td>" + deck[8] + "</td>"
+ "</tr>";
Can you see the error now (line 3)? Here is a fix:
+ "<td><img src=\"" + deck[0] + ".png\"></td>"
You could go even further to avoid repetitions (dry):
var deck = [
"KNhxd", "7CtbR", "Os8qX",
"21SKd", "CWMZC", "43C1X",
"lpGvK", "8Wk7W", "Y3JFi"
];
// preserve the global namespace with an IIFE
document.body.innerHTML = function () {
var ROOT = "http://i.stack.imgur.com/";
function toTable (deck) {
var i, html = "";
for (i = 0; i < 3; i++) {
html += toTr(deck.slice(i * 3, (i + 1) * 3));
}
return "<table>" + html + "</table>";
}
function toTr (row) {
return "<tr>" + row.map(toTd).join('') + "</tr>";
}
function toTd (cell) {
return "<td><img src=\"" + ROOT + cell + ".png\" /></td>";
}
return toTable(deck);
}();
body{background:#006600;}
img{display:block;width:35px;}
If you want to create HTML elements, then you do not just write them as a text string and insert into an existing HTML element.
You create an element node (and text node if applicable) and append that to your existing HTML element.
Here is an article on w3 schools about creatung HTML tags with JavaScript.
http://www.w3schools.com/jsref/met_document_createelement.asp
If your table is always 9 cells then why not just write the HTML out giving each cell a unique id. Then u could use code similar to what u have.
document.getElementById("cell_1").innerHTML = deck[0];
You would need to include this logic for all 9 cells / values, although you could loop through them like;
for (i = 0; i < 8; i++) {
var cellNo = i + 1;
document.getElementById("cell_" + cellNo).innerHTML = deck[i];
}
Hope this helps ;)
In addition to the unnecessary '+' at the end of your innerHTML addition,
I would suggest making your code more flexible. Instead of adding the data hardcoded to your innerHTML you can do something like this:
var endOfRowCounter = 0;
var toAppend = "";
deck.forEach(function(card){
if(endOfRowCounter % 3 == 0) {
toAppend += "<tr>";
}
toAppend += "<td><img src=\"" + card + ".png\"></td>";
if(endOfRowCounter % 3 == 2)
{
toAppend += "</tr>";
}
endOfRowCounter++;
});
When I load your code I get this message in the browser console:
Uncaught SyntaxError: Unexpected token ;
It might help if you update this code:
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>" +
;
To this:
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>";
To loop your array and create your table structure with the images, you could do this for example:
function createTable() {
var deck = [1,2,3,4,5,6,7,8,9];
function shuffle(o) { //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(deck);
var html = '';
for (var i = 0; i < deck.length; i++) {
if (i%3 === 0) {
html += "<tr>";
}
html += '<td><img src="' + deck[i] + '.png"></td>';
if (i%3 === 2) {
html += "</tr>";
}
}
document.getElementById("1").innerHTML = html;
}
first of all i need to say that i don't have much experience with JS. currently i'm trying to implement an web application with MVC framework. I'm in a work to develop an app that is also compatible with Internet explorer. in that case i'm using following JS method to populate a table which is working fine with all the browsers....
function populateTable(array) {
document.getElementById("instalationTable").style.display = "block";
var table = document.getElementById("ActivityDescription_installationID");
table.innerHTML = "";
elementsInTable = array;
var x = 0;
for (i = 0; i < (array.length * 2) ; i++) {
//alert(i);
if ((i % 2) == 0) {
//ID Row
var row = table.insertRow(i);
var cell_1 = row.insertCell(0);
cell_1.innerHTML = "<input type='text' disable='' class='form-control' value=" + array[x] + ">";
x = x + 1;
var cell_2 = row.insertCell(1);
cell_2.innerHTML = "<span class='btn btn-default' onclick='showEditRow(this)'><img src='../../Content/images/1414409386_48-24.png' /></span>";
var cell_3 = row.insertCell(2);
cell_3.innerHTML = "<span class='btn btn-default' onclick='removeRow(this)'>X</apan>";
}
else {
//Detail Row
var rowDetails = table.insertRow(i);
var cell = rowDetails.insertCell(0);
//cell.colspan = "3";
cell.innerHTML = "<table style='background-color:rgb(98, 98, 98);color:black;border- radius: 5px;' margin:2%; >" +
"<tr>" +
"<td><input type='checkbox' id='"+x+"_appServer'/> Application Server</span></td>" +
"<td>" +
"<select id='" + x + "_appServerVersion'>" +
"<option>Application version</option>" +
"</select>" +
"</td>" +
"</tr>" +
"<tr>" +
"<td colspan='2'><input type='radio' name='database' id='"+x+"_emptyDb' onChange='enableOptions(1)'/>" +
" Empty Database</br><input type='radio' name='database' id='" + x + "_instalationSlt' onChange='enableOptions(2)'/> Something Databse</td>" +
"</tr>" +
"<tr id='emptyDB'>" +
"<td>" +
"Oracle Version"+
"<select id='JS_OraVersion' name='" + x + "_oraVersion' style='width:100%'>" +
"<option>Ora version</option>" +
"</select>" +
"</td>" +
"<td>" +
"Character Set" +
"<select id='JS_ChaSet' name='" + x + "_ChaSet' style='width:100%'>" +
"<option>Cha Set</option>" +
"</select>" +
"</td>" +
"</tr>" +
"<tr id='dbImport'>" +
"<td>" +
"Something version" +
"<select id='JS_ImportVersion' name='" + x + "_ImportVersion' style='width:100%'>" +
"<option>Something version</option>" +
"</select>" +
"</td>" +
"<td>" +
"Something Charachter" +
"<select id='JS_ImportChaSet' name='" + x + "_ImportChaSet' style='width:100%'>" +
"<option>Something Cha</option>" +
"</select>" +
"</td>" +
"</tr>" +
"<tr>" +
"<td colspan='2'>" +
"Additional Requests </br>" +
"<textarea rows='4' id='" + x + "_specialReq' cols='37'> </textarea>" +
"<td/>"+
"</tr>"+
"</table>";
rowDetails.style.display = 'none';
Lock();
}
}
document.getElementById("instalationTable").style.display = "block";
}
i'm populating a form on the above table row, that collects some data to continue. to collect data i'm using following function which works fine with Google chrome but not with Internet explorer..
function getAllData() {
var StringtoSent = "";
for (i = 0; i < (elementsInTable.length) ; i++) {
var InsId = elementsInTable[i];
var _appServer = document.getElementById((i + 1) + "_appServer").checked;
var _appServerVersionDropDown = document.getElementById((i + 1) + "_appServerVersion");
var _appServerVersion = _appServerVersionDropDown.options[_appServerVersionDropDown.selectedIndex].value;
var _emptyDb = document.getElementById((i + 1) + "_emptyDb").checked;
var _instalationSlt = document.getElementById((i + 1) + "_instalationSlt").checked;
var _oraVersionDropDown = document.getElementsByName((i + 1) + "_oraVersion")[0];
var _oraVersion = _oraVersionDropDown.options[_oraVersionDropDown.selectedIndex].value;
var _ChaSetDropDown = document.getElementsByName((i + 1) + "_ChaSet")[0];
var _ChaSet = _ChaSetDropDown.options[_ChaSetDropDown.selectedIndex].value;
var _ImportVersionDropDown = document.getElementsByName((i + 1) + "_ImportVersion")[0];
var _ImportVersion = _ImportVersionDropDown.options[_ImportVersionDropDown.selectedIndex].value;
var _ImportChaSetDropDown = document.getElementsByName((i + 1) + "_ImportChaSet")[0];
var _ImportChaSet = _ImportChaSetDropDown.options[_ImportChaSetDropDown.selectedIndex].value;
var _specialReq = document.getElementById((i + 1) + "_specialReq").value;
StringtoSent = StringtoSent + "," + InsId + "," + _appServer + "," + _appServerVersion + "," + _emptyDb + "," + _instalationSlt + "," + _oraVersion + "," + _ChaSet + "," + _ImportVersion + "," + _ImportChaSet + "," + _specialReq + "|";
//return StringtoSent;
document.getElementById("ActivityDescription_instalationDetails").value = StringtoSent;
}
}
following image shows the error that im getting when it is ruining on VS 2012s IIS Express.
for (i = 0; i < (elementsInTable.length) ; i++) {
is the place that indicates as the error place . it always highlight the "elementsInTable.length" code segment.
Actually this error message elaborate nothing. i found some articles about the same error but occurring when trying to change the inner HTML of an element. but those solutions are not compatible for this situation.. Please help me with the problem
thanks in advance
Finally i found the Error
cell.innerHTML = "<table style='background-color:rgb(98, 98, 98);color:black;border- radius: 5px;' margin:2%; >" +
in above line i mistakenly added a CSS attribute "margin:2%;" in to a wrong place. Google chrome is intelligence enough to manage the situation and proceed the activity but Internet Explorer is not. as i found, this is the fact that prompt same error in different situations.
EG:
http://www.webdeveloper.com/forum/showthread.php?22946-innerHTML-amp-IE-Unknown-Runtime-Error
http://www.webdeveloper.com/forum/showthread.php?22946-innerHTML-amp-IE-Unknown-Runtime-Error
So if you got any unknown error in your Java Script code which uses "element.InnerHTML" or "document.Write" its better to check whether your tags are properly closed.
and i found several other situations that is generating same error
IE shows run time error for innerHTML
InnerHTML issue in IE8 and below
most of the times you can avoid this error by following W3C tag recommendations (http://www.w3.org/TR/html401/struct/global.html).
this is my json object
I need to get length of itemDetails array object,
I'm using
for (var i = 0; i < data[0].itemDetails.length; i++) {
itmCode = "it001";
itmName = "soap";
Qty = "4";
//netAmt = parseFloat(data[i].Net_Amt).toFixed(2);
//paidAmt = parseFloat(data[i].Paid_Amt).toFixed(2);
//balance = (parseFloat(netAmt) - parseFloat(paidAmt)).toFixed(2); //id = "damt['+i+']"
$("#invoiceDetailTbl tbody").append("<tr id=" + i + ">" + "<td>" + itmCode + "</td>" + "<td>" + itmName + "</td>" + "<td>" + Qty + "</td>" + "</tr>");
noOfItems = parseInt(noOfItems) + parseInt(Qty);
noOfProducts = parseInt(noOfProducts) + 1;
}
but always get 0 as length