programming-examples/js/Array/Write a JavaScript function to fill an array with values (numeric, string with one character) on supplied bounds..js
2019-11-15 12:59:38 +01:00

47 lines
1.3 KiB
JavaScript

function num_string_range(start, end, step)
{
var range = [];
if ((step === 0) || (typeof start == "undefined" || typeof end == "undefined") || (typeof start != typeof end))
return false;
if (end < start) {
step = -step;
}
if (typeof start == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
}
else if (typeof start == "string") {
if (start.length != 1 || end.length != 1) {
throw TypeError("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 are supported");
}
return range;
}
console.log(num_string_range('a', "z", 2));
console.log(num_string_range("Z", "A", 2));
console.log(num_string_range(0, -5, 1));
console.log(num_string_range(0, 25, 5));
console.log(num_string_range(20, 5, 5));