Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

勘误:章节 3.2.1,页码 39 #9

Open
yanhaijing opened this issue Jan 17, 2023 · 3 comments
Open

勘误:章节 3.2.1,页码 39 #9

yanhaijing opened this issue Jan 17, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@yanhaijing
Copy link
Member

yanhaijing commented Jan 17, 2023

勘误范围:第一次印刷

image

图中红色箭头处 '0' 应该替换为参数 ch,,并在map后面添加join(''),完整代码如下

function leftpad(str, count, ch = '0') {
  return `${[...Array(count)].map((v) => ch).join('')}${str}`.slice(-count);
}

上面代码,在浏览器里能够得到正确的结果,如下图:
image

@yanhaijing yanhaijing added the bug Something isn't working label Jan 17, 2023
@front-thinking
Copy link

function leftPad(str, count, ch = '0') {
    return `${[...Array(count)].map((v) => ch)}${str}`.slice(-count).join('');
}

打开console,试一下,依然不对。

@front-thinking
Copy link

补充个正解

function leftPad(str, count, ch = '0') {
    return `${[...Array(count)].map((v) => ch).slice(-(--count)).join('')}${str}`;
}

@yanhaijing
Copy link
Member Author

嗯嗯,参考你的已经修改了答案,你这个还有点小问题,count的计算不对,如下图:

image

@yanhaijing yanhaijing changed the title 勘误:章节 3.2.1,页码 勘误:章节 3.2.1,页码 39 Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants