我正在学习随机算法,我目前在一个库存中,我必须反转包含数字的字符串,但我不会在字符串中反转1和0,例如,2345678910将是1098765432.
这是我到目前为止所做的:
function split(str) {
let temp = [];
temp = str.split('');
const backwards = [];
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);
}
return backwards.join('').toString();
}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));Run Code Online (Sandbox Code Playgroud)
我目前的问题是没有扭转10.
我究竟做错了什么?
所以我有这些类来处理不同场景中的错误,如下所示:
export class APIError extends Error {
public readonly statusCode: number;
public readonly message: string;
constructor(statusCode?: number, message?: string) {
super(message);
Object.setPrototypeOf(this, APIError.prototype);
if (typeof statusCode === 'string') {
message = statusCode;
statusCode = null;
}
this.statusCode = statusCode || 500;
this.message = message || 'Internal Server Error';
}
public toJSON(): JsonData {
return {
statusCode: this.statusCode,
message: this.message,
};
}
}
export class NotFound extends APIError {
constructor(message?: string) {
super(404, 'Not Found');
Object.setPrototypeOf(this, NotFound.prototype);
}
}
export class …Run Code Online (Sandbox Code Playgroud)