Using JavaScript Calculated Values in Less

using javascript calculated values in less

Simply use string interpolation and then escape from the string using ~:

@winheight:`$(window).height()`;

height: ~"@{winheight}px";

Calculate value in array under certain condition

Just check the last value of the result set.

const
array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15],
minTen = array.reduce((accu, value) => {
if (accu[accu.length - 1] < 10) accu[accu.length - 1] += value;
else accu.push(value);
return accu;
}, []);

console.log(minTen); // [25, 11, 18, 13, 15]

Javascript calculate sum unless it is less than zero

You could take Math.max along with zero.

a = Math.max(b * 100 / c, 0);

Auto Calculate with already given value

You're attempting to calculate things using the element, not its value.

Replace

var initialcost = document.getElementById("initialcost");

with

var initialcost = parseFloat(document.getElementById("initialcost").value);

Example with automatic recalculation to boot:

var initialCostElement = document.getElementById("initialcost");
function compute() {
var initialcost = parseFloat(initialCostElement.value);
var Tfee = initialcost - (initialcost * 5)/100;
document.getElementById("fee").value = +Tfee;
}

initialCostElement.addEventListener("input", compute, false);
compute();
<input type="number" value="1000" id="initialcost">
<input type="text" readonly="readonly" id="fee">

Javascript calculations holding variables

Here it is:

function Calculate() {    var first = document.getElementById('first').value;    var sec = document.getElementById('sec').value;    var ans = document.getElementById('ans').value;
document.getElementById('ans').value = parseInt(first) + parseInt(sec); document.getElementById('ans2').value = document.getElementById('ans').value; /*document.form1.submit();*/}
<form name="Calcultor" Method="Get" id='form1'>First Number:    <input type="text" name="fnum" size="35" id="first">+ Second Number:    <input type="text" name="snum" size="35" id="sec">    <br>    <br>Answer:    <input type="text" name="ans" size="35" id="ans" />    <input type="text" name="ans2" size="35" id="ans2" />    <button type="button" onclick="Calculate();">Calculate</button></form>


Related Topics



Leave a reply



Submit