JavaScript是否具有类似“ 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;

}

jsfiddle

如果您想扩展本机类型,则将其分配给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));

解决方法

在PHP中,您可以…

range(1,3); // Array(1,2,3)
range("A","C"); // Array("A","B","C")

也就是说,有一个函数可让您通过传递上下限来获得一定范围的数字或字符。

为此,JavaScript是否内置任何内置功能?如果没有,我将如何实施?

相关文章

猜你在找的编程问答相关文章

怎样才能让 Git“忘记”一个被跟踪但现在在 .gitignore 中的文件?
Python 中的metaclasses是什么?
如何在 Linux 上查找包含特定文本的所有文件?
如何从异步调用返回响应
如何在一个表达式中合并两个字典(取字典的并集)?
HTTP 中的 POST 和 PUT 有什么区别?
使用 Git 将最近的提交移动到新分支
在 JavaScript 比较中应该使用哪个等于运算符 (== vs ===)?