3.7 KiB
3.7 KiB
title | localeTitle |
---|---|
Declare a Read-Only Variable with the const Keyword | 使用const关键字声明只读变量 |
如果卡住,请记得使用**Read-Search-Ask
** 。尝试配对程序并编写自己的代码
问题说明:
将所有变量更改为let
或const
并重命名sentence
。
提示:1
- 用只读
const
替换var
的字符串。
现在尝试解决问题
提示:1
- 更换
var
中for
循环来let
。
现在尝试解决问题
提示:1
- 常见的约定是使用ALL CAPS命名
const
变量。
现在尝试解决问题
扰流警报!
提前解决!
基本代码解决方案
function printManyTimes(str) {
"use strict";
const SENTENCE = str + " is cool!";
for(let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
}
printManyTimes("freeCodeCamp");
代码说明:
通过在sentence
上使用const
,我们可以将它设为只读,并且通过在for循环中使用let
on i
,我们可以避免一起使用var
。为了增加代码清晰度,我们还可以将sentence
更改为SENTENCE
以显示它是常量。
相关链接
捐款说明:
- 请勿添加与任何现有解决方案类似的解决方案。如果您认为它**相似但更好** ,那么尝试合并(或替换)现有的类似解决方案。
- 添加解决方案的说明。
- 将解决方案分为以下类别之一 - 基本 , 中级和高级 。
- 如果您添加了任何**相关的主要内容,**请仅添加您的用户名。 ( 不要 删除任何现有的用户名 )