2 回答
TA贡献1900条经验 获得超5个赞
简单的方法如下:
function noManagement(code, digit, input) {
if (arguments.length < 3) throw "Not enough arguments";
for (var i = 0; i < 3; i++) {
if (arguments[i] == null) throw "Bad argument";
}
var inputArr = input.split("");
let result = null;
for (var i = 0; i < digit; i++) {
inputArr.unshift(0);
if (inputArr.length === digit) {
result = code + inputArr.join("");
}
}
return result;
}
虽然,因为您的参数应该是字符串、数字、字符串
function noManagement(code, digit, input) {
if (typeof code !== 'string') throw "first argument should be String";
if (typeof digit !== 'number') throw "second argument should be Number";
if (typeof input !== 'string') throw "third argument should be String";
var inputArr = input.split("");
let result = null;
for (var i = 0; i < digit; i++) {
inputArr.unshift(0);
if (inputArr.length === digit) {
result = code + inputArr.join("");
}
}
return result;
}
TA贡献1806条经验 获得超5个赞
这是一个简单的添加,将检查未定义。如果未提供三个中的任何一个,它将停止执行该函数
function noManagement(code, digit, input) {
if (!code) throw new Error("code is undefined");
if (!digit) throw new Error("digit is undefined");
if (!input) throw new Error("input is undefined");
var inputArr = input.split("");
let result = null;
for (var i = 0; i < digit; i++) {
inputArr.unshift(0);
if (inputArr.length === digit) {
result = code + inputArr.join("");
}
}
return result;
}
添加回答
举报