Adding all columns together? - javascript

function subTotals() {
var otTableTotal = 0;
var sTableTotal = 1;
var r = parseFloat($('#rate').val());
$('.ot').each(function(index) {
if ($(this).data('mins') !== undefined) {
otTableTotal += $(this).data('mins');
}
});
$('#ot_subtotal').html(convertToHours(otTableTotal));
$('#otE_subtotal').text((((otTableTotal / 60) * r) * 1.5).toFixed(2));
var hoursTableTotal = 0;
$('.hours').each(function(index) {
if ($(this).data('mins') !== undefined) {
hoursTableTotal += $(this).data('mins');
}
});
$('.sick').each(function(index) {
sTableTotal += parseInt($(this).val());
});
$('#s_subtotal',this).html(sTableTotal);
$('#hours_subtotal').html(convertToHours(hoursTableTotal));
//$('#tE_subtotal').text(((hoursTableTotal / 60) * r).toFixed(2));
$('#tE_subtotal').text(((((hoursTableTotal / 60) * r)) +(((otTableTotal / 60) * r) * 1.5)).toFixed(2));
$('#rE_subtotal').text((((hoursTableTotal / 60) * r)).toFixed(2));
}
I am having trouble getting my sick column to add up and display where totals is. (The first 2 zeroes are for Hours and Overtime Hours which I have working.) I just can't get this darned sick thing to work.

Try removing the context (this) here:
$('#s_subtotal',this).html(sTableTotal);

Related

Printing Triangle star(*) pattern in javascript using Recursion [duplicate]

This question already has answers here:
Is there any way to console.log without a newline?
(5 answers)
Closed 3 months ago.
I have been trying to solve printing down left side star(*) pattern in Javascript using recursion, i think my logic is correct but my syntax and concept might be wrong
// * * * * *
// * * * *
// * * *
// * *
// *
This is my code solution so far
var triangle = function (row, col) {
if(row == 0){
return
}
if(col < row){
console.log("*")
triangle(row, col + 1)
}else{
console.log("\n")
triangle(row - 1, 0)
}
}
triangle(4, 0)
output
*
*
*
*
*
*
*
*
*
*
But i want the output to be
* * * * *
* * * *
* * *
* *
*
console.log() closes the output stream automatically, the "line break" is because it is the next output session. You will see its true form if you use document.write.
var triangle = function (row, col) {
if(row == 0){
return
}
if(col < row){
document.write("*")
triangle(row, col + 1)
}else{
document.write("<br/>")
triangle(row - 1, 0)
}
}
triangle(4, 0)
But if you insist to use console.log(), some modification must be made:
var triangle = function (row, col, stream = '') {
if(row == 0){
return
}
if(col < row){
stream += '*'
triangle(row, col + 1, stream)
}else{
console.log(stream)
stream = ''
triangle(row - 1, 0, stream)
}
}
triangle(4, 0)
check this solution
function triangle(row){
var print="";
if(row==0){
return;
}
for(var i=0;i<row;i++){
print+="* "
}
console.log(print);
triangle(row-1);
}
triangle(5);

JS Why are these booleans returning wrong values?

For some reason, the boolean meaningfulIndexes.indexOf(index) != -1 returns false 100% of the time, and I suspect that the arrayFindNext does also. Why could this be? finalProcessing is an array that has a fixed size of 64, and bars has a length of 128.
meaningfulIndexes = [];
for (let i = 0; i < 64; i++) {
meaningfulIndexes.push((bars.length / 64) + i * (bars.length / 64))
}
bars.forEach((currentValue, index, array) => {
if (index == 0) {
array[index].desiredHeight = finalProcessing[0] * canvas.height / 3 + 1;
}
if (meaningfulIndexes.indexOf(index) != -1) {
array[index].desiredHeight = finalProcessing[meaningfulIndexes.indexOf(index)] * canvas.height / 3 + 1;
}
else {
array[index].desiredHeight = (((array[index - 1].desiredHeight - 1) * 3 / canvas.height + finalProcessing[meaningfulIndexes.findIndex(arrayFindNext(index))]) / 2) * canvas.height / 3 + 1;
}
})
function arrayFindNext(value) {
return value > i;
}

Memory issues in recursion

I'm trying to solve this leetcode problem here: https://leetcode.com/problems/next-closest-time/description/.
Simply my solution is to try every combination that is valid and then set my ans variable to the one that is closest to the original time. It does print out all the combinations however the diff variable never changes. In the for loop after (if cur.length ==4) it prints every possible combination because it still thinks that diff is 9007199254740991. Also when I assign ans to cur.slice(), I receive a blank array. Even when I uncomment it in the nextClosestTime function I still have the same issue.
/**
* #param {string} time
* #return {string}
*/
var diff = 9007199254740991;
function calcSeconds(digits) {
var hours = (digits[0] * 10) + digits[1];
var seconds = (digits[2] * 10) + digits[3];
return (hours * 3600) + (seconds * 60);
}
function nextClosestTime(time) {
var digits = [];
// diff = 9007199254740991;
var cur = [];
var ans = [];
for (var i = 0; i < time.length; i++) {
if (isFinite(time.charAt(i))) {
digits.push(parseInt(time.charAt(i)));
}
}
var target = calcSeconds(digits);
nch(digits, cur, diff, target, ans);
return ans;
};
function nch(digits, cur, diff, target, ans) {
if (cur.length == 4) {
var curSec = calcSeconds(cur);
if (Math.abs(curSec - target) < diff) {
console.log(cur);
diff = Math.abs(calcSeconds(digits) - calcSeconds(cur));
//ans = cur.slice();
}
return;
}
for (var i = 0; i < digits.length; i++) {
if ((((cur[0] * 10) + cur[1]) >= 24) || (((cur[2] * 10) + cur[3]) >= 60)) {
return;
}
cur.push(digits[i]);
nch(digits, cur, diff, target, ans);
cur.pop()
}
}

Calculate the nearest value on a circular variable

I have an problem where i have 3 times of the 24 hour day. To keep it simple i can use the decimal representation:
a) 23:45 (23.75)
b) 11:30 (11.50)
c) 00:15 (00.25)
I'd like to know , for each time, which other time is closest.
var closestTime = 24
var closestActualTime = 0;
for (var i = 0; i < times.length; i++) {
if (times[i].time == this.time) continue;
var temp = Math.abs(this.time - times[i].time)
if (temp < closestTime) {
closestTime = temp;
closestActualTime = times[i].time;
}
}
My issue is that 23:45 and 00:25 are actually really close but i don't know how process a variable with a modulo type
I suggest to build a list with the pairs and then calculate the difference.
The difference is the third element in the pairs array.
Basically you need to check the delta and if it greater than 12 hours, take the difference from 24 and delta.
delta = Math.abs(aa - bb);
if (delta > 12) {
delta = 24 - delta;
}
function combination(array, size) {
function c(part, start) {
var i, l, p;
for (i = start, l = array.length + part.length + 1 - size; i < l; i++) {
p = part.slice();
p.push(array[i]);
p.length < size ? c(p, i + 1) : result.push(p);
}
}
var result = [];
c([], 0);
return result;
}
function timeDelta(a, b) {
function decimalTime(s) {
var p = s.split(':');
return +p[0] + p[1] / 60;
}
function padZero(v) {
return (v < 10) ? '0' + v : String(v);
}
var aa = decimalTime(a),
bb = decimalTime(b),
delta = Math.abs(aa - bb);
if (delta > 12) {
delta = 24 - delta;
}
return padZero(Math.floor(delta)) + ':' + padZero(Math.round(60 * (delta - Math.floor(delta))));
}
var times = ['23:45', '11:30', '00:15'],
pairs = combination(times, 2);
pairs.forEach(function (a, i, aa) {
aa[i][2] = timeDelta(a[0], a[1]);
});
console.log(pairs);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Loop over times.
Try combinations of delta time, offset by 24 hours.
Pick smallest delta time.
var times = [23.75, 11.50, 3, 6, 7];
/**
* timeClosestTo
*
* #param {number} time
* #returns {number}
*/
function timeClosestTo(time) {
//Distance variable to compare against
var distance = 100;
//Hours in a day
var day = 24;
//Current best
var best = null;
//Test against all times
for (var i = 0; i < times.length; i++) {
//Find best score based upon day
var d = Math.min(Math.abs((times[i]) - (time)), Math.abs((times[i] + day) - time), Math.abs((times[i]) - (time + day)), Math.abs((times[i] + day) - (time + day)));
//If best found distance yet, set best to current
if (d < distance) {
best = times[i];
distance = d;
}
}
//Return best
return best;
}
console.log("times to deal with:",times.join(", "));
console.log("closest to 1:", timeClosestTo(1), "closest to 11:", timeClosestTo(11), "closest to 5:", timeClosestTo(5));
Quite functionally i would do this job as follows
var times = [23.75,11.50,0.25],
diffs = times.reduce((d,t1,i,a) => a[i+1] ? d.concat(a.slice(i+1)
.map(t2 => [t1,t2,[Math.min(Math.abs(t1-t2),24-Math.abs(t1-t2))]
.map(n => ~~n + ":" + (n%1)*60)[0]]))
: d,[]);
console.log(diffs);

Javascript loop calculation

Using the below script
var lvl = 0;
var HappB = 5;
var DecoX = 5;
var DecoY = 5;
while (lvl < 5) {
document.ofrm.UpgSD.value += Math.ceil((Math.exp((HappB + lvl) / ((DecoX * DecoY) * 1.8))) * 1 * 130000 * (Math.tanh((lvl + 1) / 20)) * (Math.tanh((lvl + 1) / 20)));
lvl++;
}
<form name="ofrm">
<input type="text" name="UpgSD" size="50" tabindex="1">
</form>
The result is
363147633676050952513778
The expected output is
363 + 1476 + 3367 + 6050 + 9525 + 13778 = 34559
How can I fix this?
Here's an updated code.
1. Your loop condition needs to be corrected.
2. .value is string. You can set to a variable and then attach it.
var lvl = 0;
var HappB = 5;
var DecoX = 5;
var DecoY = 5;
var number = 0;
while (lvl <= 5) {
number += Math.ceil((Math.exp((HappB + lvl) / ((DecoX * DecoY) * 1.8))) * 1 * 130000 * (Math.tanh((lvl + 1) / 20)) * (Math.tanh((lvl + 1) / 20)));
lvl++;
}
document.ofrm.UpgSD.value = number;
<form name="ofrm">
<input type="text" name="UpgSD" size="50" tabindex="1">
</form>
It looks like document.ofrm.UpgSD.value is being cast into a string (rather than a number).
var lvl = 0;
var HappB = 5;
var DecoX = 5;
var DecoY = 5;
var initialValue = parseInt(document.ofrm.UpgSD.value);
while (lvl < 5) {
initialValue += Math.ceil((Math.exp((HappB + lvl) / ((DecoX * DecoY) * 1.8))) * 1 * 130000 * (Math.tanh((lvl + 1) / 20)) * (Math.tanh((lvl + 1) / 20)));
lvl++;
}
document.ofrm.UpgSD.value = initialValue;

Categories

Resources