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};
}
This question already has answers here:
JavaScript: Format whole numbers using toLocaleString()
(4 answers)
Closed 1 year ago.
I am creating a calculator and need guidance on adding thousand separators to a whole number.
//logic
var portVal = 100000000;
var rateUSD = 1.3654;
var sustPortBase = 22;
var avgPortBase = 66;
var sustPortCarb = (portVal*rateUSD/1000000)*sustPortBase;
var avgPortCarb = (portVal*rateUSD/1000000)*avgPortBase;
var diff = avgPortCarb - sustPortCarb;
var nbHomes = diff * 0.115;
var nbCars = diff * 0.216;
var nbTrees = diff * 16.5;
var nbPlanes = diff * 0.833359;
var avgPortCarbRound = avgPortCarb.toFixed();
var sustPortCarbRound = sustPortCarb.toFixed();
console.log("Cars:"+(nbCars).toFixed());
console.log("Cars Decimals:"+(nbCars).toLocaleString('en'))
Outputs:
Cars:1298
Cars Decimals:1,297.676
I would like to keep only thousand seperators but nothing after the (dot.) i.e. 1,297
https://jsfiddle.net/ge5oj2xk/1/
Try using Math.floor function.
console.log("Cars Decimals:"+Math.floor(nbCars).toLocaleString('en'))
This question already has answers here:
Plus operator in JavaScript doesn't sum my values
(4 answers)
Closed 3 years ago.
function calculateAmount(val) {
var price = val;
if (quantity >= 100 && quantity < 1000) {
var divobj = document.getElementById('discount');
divobj.value = 20;
var divobj1 = document.getElementById('total');
var total = ((20 / 100) * parseInt(price)) + parseInt(price);
divobj1.value = total;
}
}
For example if enter the quantity to be 100 then the output return is 10020 instead 100+20 = 120
var price = val;
because the val is a string:
var price = +val;
please test this code, I hope this code works
divobj.value = 20;
please delete above line code:
var total = ((divobj.value / 100) * parseInt(price)) + parseInt(price);
and use above line code instead of use below code:
var total = ((20 / 100) * parseInt(price)) + parseInt(price);
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I am trying to make a calculator that calculates the amount of calories that have been burned in a certain amount of time, but whenever I run it, I get 0 as the output. I have tried setting the calorieCountOut variable to an arbitrary number, and then it works fine, but every time I run it with this code here, I get 0. Here is my code:
const AGECONST = 0.2017;
const WEIGHTCONST = 0.09036;
const HRCONST = 0.6309;
const SUBTRACTCONST = 55.0969;
const TIMECONST = 4.184;
//var gender = document.getElementById("gender").innerHTML;
var gender = "male";
var weight = document.getElementById("weight");
var age = document.getElementById("age");
var time = document.getElementById("time");
var hr = 140;//dummy number
function calculate(){
if (gender = "male"){
var calorieCount = ((age * AGECONST) - (weight * WEIGHTCONST) + (hr * HRCONST) - SUBTRACTCONST) * time / TIMECONST;
}
//else if (gender = "female"){
//}
var calorieCountOut = calorieCount.toString();
document.getElementById("output").innerHTML = calorieCountOut;
}
Try getElementById('myId').value for the value inside the control instead of the control itself as an object.
Right now you assign a html object to a variable, but what you want (i assume) is the number stored in that html object.
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);
})()