小编use*_*351的帖子

如何使用数字反转字符串,但不要反转1和0?

我正在学习随机算法,我目前在一个库存中,我必须反转包含数字的字符串,但我不会在字符串中反转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.

我究竟做错了什么?

javascript algorithm

12
推荐指数
1
解决办法
939
查看次数

为什么 typescript 中的 instanceof 没有按预期工作?

所以我有这些类来处理不同场景中的错误,如下所示:

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)

javascript prototype typescript

1
推荐指数
1
解决办法
3092
查看次数

标签 统计

javascript ×2

algorithm ×1

prototype ×1

typescript ×1