I'm quite new to Javascript, and need to find the min and max value of an array from user input, but I'm having difficulty with that. I tried to get the max value with this:
var max = Math.max.apply(Math, data);
but it didn't work.
Here is what I have so far:
var data = [];
var productInput = document.getElementById("product");
var priceInput = document.getElementById("price");
var qtyInput = document.getElementById("qty");
var messageBox = document.getElementById("display");
function insert() {
var product = productInput.value;
var price = priceInput.value;
var qty = qtyInput.value;
data.push({
product: product,
price: price,
qty: qty,
});
clearAndShow();
}
function clearAndShow() {
// Clear our fields
productInput.value = "";
priceInput.value = "";
qtyInput.value = "";
var html = "";
// Show our output html += "<tr>"; html += "<td>Product</td>";
html += "<td>price</td>";
html += "<td>quantity</td>";
html += "</tr>";
for (i = 0; i <= data.length - 1; i++) {
html += "<tr>";
html += "<td>" + data[i].product + "</td>";
html += "<td>" + data[i].price + "</td>";
html += "<td>" + data[i].qty + "</td>";
html += "</tr>";
}
messageBox.innerHTML = html;
}
<form>
<h1>Please enter data</h1>
<input id="product" type="text" placeholder="product" />
<input id="price" type="text" placeholder="price" />
<input id="qty" type="text" placeholder="qty" />
<input type="button" value="Save/Show" onclick="insert()" />
</form>
<table id="display" style="width: 100%"></table>