Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
If I have a list of arrays as follows, what would be the quickest way to check if today i.e. 2022-03-08T23:30:00.000Z is within 5 days of any of the future dates and return true along with the nearest future date (within a 5 day window)?
For example, in this example, it would return 2022-03-13T13:00:00.000Z as the nearest future date and return as true because it falls under this date.
export const nextRotaSchedules = [
'2021-12-13T12:00:00.000Z',
'2021-12-13T14:00:00.000Z',
'2022-01-13T14:00:00.000Z',
'2022-01-23T14:00:00.000Z',
'2022-01-24T13:00:00.000Z',
'2022-02-12T17:02:33.000Z',
'2022-02-12T17:03:59.000Z',
'2022-02-12T17:36:18.000Z',
'2022-02-12T17:36:48.000Z',
'2022-02-12T17:37:22.000Z',
'2022-02-12T17:37:45.000Z',
'2022-02-13T13:00:00.000Z',
'2022-02-13T13:49:16.000Z',
'2022-02-13T14:00:00.000Z',
'2022-03-13T13:00:00.000Z',
'2022-04-13T13:00:00.000Z',
'2022-05-13T13:00:00.000Z',
'2022-06-13T13:00:00.000Z',
'2022-07-13T13:00:00.000Z',
'2022-08-13T13:00:00.000Z',
'2022-09-13T13:00:00.000Z',
'2022-10-13T13:00:00.000Z',
'2022-11-13T14:00:00.000Z',
'2022-12-13T14:00:00.000Z',
'2023-01-13T14:00:00.000Z',
'2023-02-13T14:00:00.000Z',
'2023-03-13T13:00:00.000Z',
'2023-04-13T13:00:00.000Z',
'2023-05-13T13:00:00.000Z',
'2023-06-13T13:00:00.000Z',
'2023-07-13T13:00:00.000Z',
'2023-08-13T13:00:00.000Z',
'2023-09-13T13:00:00.000Z',
'2023-10-13T13:00:00.000Z',
'2023-11-13T14:00:00.000Z',
'2023-12-13T14:00:00.000Z',
'2024-01-13T14:00:00.000Z',
];
const nextRotaSchedules = [
'2021-12-13T12:00:00.000Z',
'2021-12-13T14:00:00.000Z',
'2022-01-13T14:00:00.000Z',
'2022-01-23T14:00:00.000Z',
'2022-01-24T13:00:00.000Z',
'2022-02-12T17:02:33.000Z',
'2022-02-12T17:03:59.000Z',
'2022-02-12T17:36:18.000Z',
'2022-02-12T17:36:48.000Z',
'2022-02-12T17:37:22.000Z',
'2022-02-12T17:37:45.000Z',
'2022-02-13T13:00:00.000Z',
'2022-02-13T13:49:16.000Z',
'2022-02-13T14:00:00.000Z',
'2022-03-13T13:00:00.000Z',
'2022-04-13T13:00:00.000Z',
'2022-05-13T13:00:00.000Z',
'2022-06-13T13:00:00.000Z',
'2022-07-13T13:00:00.000Z',
'2022-08-13T13:00:00.000Z',
'2022-09-13T13:00:00.000Z',
'2022-10-13T13:00:00.000Z',
'2022-11-13T14:00:00.000Z',
'2022-12-13T14:00:00.000Z',
'2023-01-13T14:00:00.000Z',
'2023-02-13T14:00:00.000Z',
'2023-03-13T13:00:00.000Z',
'2023-04-13T13:00:00.000Z',
'2023-05-13T13:00:00.000Z',
'2023-06-13T13:00:00.000Z',
'2023-07-13T13:00:00.000Z',
'2023-08-13T13:00:00.000Z',
'2023-09-13T13:00:00.000Z',
'2023-10-13T13:00:00.000Z',
'2023-11-13T14:00:00.000Z',
'2023-12-13T14:00:00.000Z',
'2024-01-13T14:00:00.000Z',
];
const isWithin5Days = (today, futureDates) => {
let isWithin5Days = false;
let nearestFutureDate = '';
for (let i = 0; i < futureDates.length; i++) {
const futureDate = futureDates[i];
const difference = new Date(futureDate) - today;
if (difference > 0 && difference < 5 * 24 * 60 * 60 * 1000) {
isWithin5Days = true;
nearestFutureDate = futureDate;
break;
}
}
return { isWithin5Days, nearestFutureDate };
}
console.log(isWithin5Days(new Date(), nextRotaSchedules));
Strongly typed alternate:
const isWithin5Days = (futureDates: string[]) => {
const today = new Date();
const futureDatesWithToday = futureDates.map((date) => {
return {
date,
diff: Math.abs(new Date(date).getTime() - today.getTime()),
};
});
const sortedFutureDatesWithToday = futureDatesWithToday.sort(
(a, b) => a.diff - b.diff
);
const nearestFutureDate = sortedFutureDatesWithToday[0].date;
const diff = Math.abs(new Date(nearestFutureDate).getTime() - today.getTime());
return {[nearestFutureDate]: diff < 5 * 24 * 60 * 60 * 1000};
}
On my pricing page, I've got a pricing estimator; input an order value and a return rate (%) and price estimate will print into a div block below. The formula should look like this: ((orderValue * 0.05, 2) + 4) + ((returnRate / 100) * 4)... Can anyone see why this isn't working?
var elDeliveryPrice = document.getElementById('deliveryPrice');
var elOrderValue = document.getElementById('orderValue');
var elReturnRate = document.getElementById('returnRate');
var formatter = new Intl.NumberFormat('gb-GB', { style: 'currency', currency: 'GBP' });
elOrderValue.addEventListener('keyup', _ => {
let curVal = elOrderValue.value;
let curValFloat = parseFloat(curVal);
if (isNaN(curValFloat)) {
elDeliveryPrice.innerHTML = '';
return;
}
elDeliveryPrice.innerHTML = formatter.format(Math.max(curValFloat * 0.05,2) + 4) + (parseInt("elReturnRate") / 100)) * 4;
});
There are a few things that cause your issues. For starters you should also listen to 'keyup' on the returnRate input box. Secondly you should handle NaN of the returnRate value.
For example:
var elDeliveryPrice = document.getElementById('deliveryPrice');
var elOrderValue = document.getElementById('orderValue');
var elReturnRate = document.getElementById('returnRate');
var formatter = new Intl.NumberFormat('gb-GB', { style: 'currency', currency: 'GBP' });
function calc() {
let curVal = elOrderValue.value;
let curValFloat = parseFloat(curVal);
let curRate = parseFloat(elReturnRate.value);
if (isNaN(curValFloat) || isNaN(curRate)) {
elDeliveryPrice.innerHTML = '£ _';
return;
}
elDeliveryPrice.innerHTML = formatter.format(Math.max(curValFloat * 0.05,2) + 4 + (curRate / 100) * 4);
}
elOrderValue.addEventListener('keyup', calc);
elReturnRate.addEventListener('keyup', calc);
You can divide a percent number by 100 to get the correct number to include in your calculation. If the return rate input is given in text, you can convert to a number using parseInt(returnRate) e.g.
Math.max(curValFloat * 0.05,2) + 4 + (parseInt(returnRate) / 100) * 4
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the next code in javascript:
var exponential = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746;
var numerator, denominator;
function fact(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
function poisson(k, landa) {
exponentialPower = Math.pow(exponential, -landa); // negative power k
landaPowerK = Math.pow(landa, k); // Landa elevated k
numerator = exponentialPower * landaPowerK;
denominator = fact(k); // factorial of k.
return (numerator / denominator);
}
I need parse to php but i don't know how...
Can somebody help me?
you should put below variables within poisson function also dont think you need to initialize $numerator and $dominator to 0;
$exponential = 2;
$numerator = 0;
$dominator = 0;
function fact($x) {
if($x==0) {
return 1;
}
return $x * fact($x-1);
}
function poisson($k, $landa)
{
$exponential = 2;
$exponentialPower = pow($exponential, -$landa);
$landaPowerK = pow($landa,$k);
$numerator = $exponentialPower * $landaPowerK;
$dominator = fact($k);
echo ($numerator / $dominator);
}
poisson(1,2);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to make some calculations between inputs , I have 7 inputs and what I want to do is making calculations between them , so this is my code
$('.empCosts').on('input', function () {
var perHour = parseInt($('#per_hour').val());
var hours = parseInt($('#hour').val());
var perDay = parseInt($('#per_day').val());
var days = parseInt($('#day').val());
var perMonth = parseInt($('#per_month').val());
var month = parseInt($('#months').val());
var perYear = parseInt($('#per_year').val());
var perdayVal = parseInt(perHour * hours);
var perMonthVal = parseInt(perDay * days);
var perYearVal = parseInt(perMonth * month);
var perHourVal = parseInt(perDay / hours);
var perMonthDivide = parseInt(perYearVal / month);
$("#per_day").val(perdayVal);
$("#per_month").val(perMonthVal);
$("#per_year").val(perYearVal);
$("#per_hour").val(perHourVal);
if($(this).is(":focus")){
alert('sdfasdfad');
}
});
these inputs should be connected either you multiply or divide , when I try to change per_day value it doesn't change because I already set the value of that input with var perdayVal = parseInt(perHour * hours);, I want either you increase the number or decrease it , it should make the calculation either multiplying or dividing the inputs, here you have the fiddle
DEMO
try this https://jsfiddle.net/2jb2e3Lv/7/
$('.empCosts').on('input', function () {
var perHour = parseInt($('#per_hour').val());
var hours = parseInt($('#hour').val());
var perDay = parseInt($('#per_day').val());
var days = parseInt($('#day').val());
var perMonth = parseInt($('#per_month').val());
var month = parseInt($('#months').val());
var perYear = parseInt($('#per_year').val());
var perdayVal = perHour * hours;
var perMonthVal = perDay * days;
var perYearVal = perMonth * month;
$(this).attr("id") !== "per_day" && $("#per_day").val(perdayVal);
$(this).attr("id") !== "per_month" && $("#per_month").val(perMonthVal);
$(this).attr("id") !== "per_year" && $("#per_year").val(perYearVal);
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I am creating a web app and I get some data.
There are points you have now (now=284), and total points (total=1000). So, The difference between them is dif=716.
How do I use javascript to turn the difference into a percentage value, for example, 32% or whatever?
Thanks
This is a math question, not programming, but you ask for javascript here you are an example:
var value = 249;
var total = 1000;
var calcPercent = function(v, t) {
return 100*v/t;
};
alert(calcPercent(value, total)+"%");
var total = 1000,
subtract = 284;
var differencePercentage = ((total - subtract) / total) * 100; // 71.6
Simple math would be
var now = 284;
var total = 1000;
console.log( "percentage is " + (now* 100/total) );
console.log( "Negative percentage is " + (100-(now* 100/total)) );
(function(){
var now = 284;
var total = 1000;
var difference = ((total - now) / total) * 100;
alert(difference);
})()