Commit d159d439 authored by Pithon Kabiro's avatar Pithon Kabiro
Browse files

New function: calculate index of start timestamp

parent acbe3335
......@@ -60,19 +60,36 @@ const checkIfLeapYear = function (year) {
* @returns {Number} An integer representing the index of the timestamp of interest in the array of timestamps
*/
const getIndexOfTimestamp = function (inputTimestampArr, timestampOfInterest) {
const timestampIndex = inputTimestampArr.findIndex(
return inputTimestampArr.findIndex(
(timestamp) => timestamp === timestampOfInterest
);
};
/**
* Calculate the index of a start timestamp in an array of timestamps
*
* @param {Array} inputTimestampArr An array of timestamps, extracted from an array of observations
* @param {String} timestampOfInterest A string representing the timestamp of interest in ISO 8601 format
* @returns {Number} An integer representing the index of the timestamp of interest in the array of timestamps
*/
const getIndexOfStartTimestamp = function (
inputTimestampArr,
timestampOfInterest
) {
const timestampStartIndex = getIndexOfTimestamp(
inputTimestampArr,
timestampOfInterest
);
// If the timestamp does not exist in the timestamp array
if (timestampIndex === -1) {
if (timestampStartIndex === -1) {
throw new Error(
"A start or end timestamp could not be found in the timestamp array"
"A start timestamp could not be found in the timestamp array"
);
}
// If the timestamp exists in the timestamp array
else {
return timestampIndex;
return timestampStartIndex;
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment