Does JavaScript Have a Method Like "Range()" to Generate a Range Within the Supplied Bounds

Does JavaScript have a method like range() to generate a range within the supplied bounds?

It works for characters and numbers, going forwards or backwards with an optional step.

var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;

if (step === 0) {
throw TypeError("Step cannot be zero.");
}

if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}

typeof step == "undefined" && (step = 1);

if (end < start) {
step = -step;
}

if (typeofStart == "number") {

while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}

} else if (typeofStart == "string") {

if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}

start = start.charCodeAt(0);
end = end.charCodeAt(0);

while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}

} else {
throw TypeError("Only string and number types are supported");
}

return range;

}

jsFiddle.

If augmenting native types is your thing, then assign it to Array.range.

var range = function(start, end, step) {

var range = [];

var typeofStart = typeof start;

var typeofEnd = typeof end;

if (step === 0) {

throw TypeError("Step cannot be zero.");

}

if (typeofStart == "undefined" || typeofEnd == "undefined") {

throw TypeError("Must pass start and end arguments.");

} else if (typeofStart != typeofEnd) {

throw TypeError("Start and end arguments must be of same type.");

}

typeof step == "undefined" && (step = 1);

if (end < start) {

step = -step;

}

if (typeofStart == "number") {

while (step > 0 ? end >= start : end <= start) {

range.push(start);

start += step;

}

} else if (typeofStart == "string") {

if (start.length != 1 || end.length != 1) {

throw TypeError("Only strings with one character are supported.");

}

start = start.charCodeAt(0);

end = end.charCodeAt(0);

while (step > 0 ? end >= start : end <= start) {

range.push(String.fromCharCode(start));

start += step;

}

} else {

throw TypeError("Only string and number types are supported");

}

return range;

}

console.log(range("A", "Z", 1));

console.log(range("Z", "A", 1));

console.log(range("A", "Z", 3));

console.log(range(0, 25, 1));

console.log(range(0, 25, 5));

console.log(range(20, 5, 5));

Does JavaScript have a method like range() to generate a range within the supplied bounds?

It works for characters and numbers, going forwards or backwards with an optional step.

var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;

if (step === 0) {
throw TypeError("Step cannot be zero.");
}

if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}

typeof step == "undefined" && (step = 1);

if (end < start) {
step = -step;
}

if (typeofStart == "number") {

while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}

} else if (typeofStart == "string") {

if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}

start = start.charCodeAt(0);
end = end.charCodeAt(0);

while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}

} else {
throw TypeError("Only string and number types are supported");
}

return range;

}

jsFiddle.

If augmenting native types is your thing, then assign it to Array.range.

var range = function(start, end, step) {

var range = [];

var typeofStart = typeof start;

var typeofEnd = typeof end;

if (step === 0) {

throw TypeError("Step cannot be zero.");

}

if (typeofStart == "undefined" || typeofEnd == "undefined") {

throw TypeError("Must pass start and end arguments.");

} else if (typeofStart != typeofEnd) {

throw TypeError("Start and end arguments must be of same type.");

}

typeof step == "undefined" && (step = 1);

if (end < start) {

step = -step;

}

if (typeofStart == "number") {

while (step > 0 ? end >= start : end <= start) {

range.push(start);

start += step;

}

} else if (typeofStart == "string") {

if (start.length != 1 || end.length != 1) {

throw TypeError("Only strings with one character are supported.");

}

start = start.charCodeAt(0);

end = end.charCodeAt(0);

while (step > 0 ? end >= start : end <= start) {

range.push(String.fromCharCode(start));

start += step;

}

} else {

throw TypeError("Only string and number types are supported");

}

return range;

}

console.log(range("A", "Z", 1));

console.log(range("Z", "A", 1));

console.log(range("A", "Z", 3));

console.log(range(0, 25, 1));

console.log(range(0, 25, 5));

console.log(range(20, 5, 5));

Does JavaScript have a method like range() to generate a range within the supplied bounds?

It works for characters and numbers, going forwards or backwards with an optional step.

var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;

if (step === 0) {
throw TypeError("Step cannot be zero.");
}

if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}

typeof step == "undefined" && (step = 1);

if (end < start) {
step = -step;
}

if (typeofStart == "number") {

while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}

} else if (typeofStart == "string") {

if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}

start = start.charCodeAt(0);
end = end.charCodeAt(0);

while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}

} else {
throw TypeError("Only string and number types are supported");
}

return range;

}

jsFiddle.

If augmenting native types is your thing, then assign it to Array.range.

var range = function(start, end, step) {

var range = [];

var typeofStart = typeof start;

var typeofEnd = typeof end;

if (step === 0) {

throw TypeError("Step cannot be zero.");

}

if (typeofStart == "undefined" || typeofEnd == "undefined") {

throw TypeError("Must pass start and end arguments.");

} else if (typeofStart != typeofEnd) {

throw TypeError("Start and end arguments must be of same type.");

}

typeof step == "undefined" && (step = 1);

if (end < start) {

step = -step;

}

if (typeofStart == "number") {

while (step > 0 ? end >= start : end <= start) {

range.push(start);

start += step;

}

} else if (typeofStart == "string") {

if (start.length != 1 || end.length != 1) {

throw TypeError("Only strings with one character are supported.");

}

start = start.charCodeAt(0);

end = end.charCodeAt(0);

while (step > 0 ? end >= start : end <= start) {

range.push(String.fromCharCode(start));

start += step;

}

} else {

throw TypeError("Only string and number types are supported");

}

return range;

}

console.log(range("A", "Z", 1));

console.log(range("Z", "A", 1));

console.log(range("A", "Z", 3));

console.log(range(0, 25, 1));

console.log(range(0, 25, 5));

console.log(range(20, 5, 5));

Add recurring date and time within range(StartDate and EndDate) using momentjs or javascript

We can use Date.getUTCDate() and Date.setUTCDate() to advance a date by a number of days, in this case seven.

We can then use a while loop to populate the result array. I'm returning an array of Date objects here, one could use .toISOString() to convert to strings.

let startDate = '2021-10-05T00:00:00Z';
let endDate = '2021-10-31T00:00:00Z';

function getWeeklyDates(start, end) {
let date = new Date(start);
const endDate = new Date(end);

const result = [];
while (date < endDate) {
result.push(date);
date = new Date(date);
date.setUTCDate(date.getUTCDate() + 7);
}
return result;
}

console.log(getWeeklyDates(startDate, endDate).map(dt => dt.toISOString()))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Vue 2 API calls in one method

By using async/await.

Edit: In this case they will be called in a sequence, the second one will be made after the first one is resolved (this is useful only in cases when you need data from the first req to make the second req).
If you want to make them in the same time you will have to use Promise.all() or Promise.allSettled().

Example:

    async apiCall() {
this.Loading = true;
try {
const resultOne = await get('lots/path1', { ... });
console.log(resultOne);

const resultTwo = await get('lots/path2', { ... });
console.log(resultTwo);
} catch (err) {
console.error(err);
} finally {
this.Loading = false;
}
}


Related Topics



Leave a reply



Submit