freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/understand-string-immutabil...

1.7 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244ba Understand String Immutability 1 理解字符串不变性

Description

在JavaScript中 String值是不可变的 ,这意味着一旦创建它们就不能被更改。例如,以下代码:
var myStr =“Bob”;
myStr [0] =“J”;
无法将myStr的值更改为“Job”因为myStr的内容无法更改。请注意,这并不意味着myStr不能改变的,只是一个字符串的单个字符不能改变。更改myStr的唯一方法是为其分配一个新字符串,如下所示:
var myStr =“Bob”;
myStr =“工作”;

Instructions

更正myStr的赋值,使其包含Hello World的字符串值,使用上面示例中显示的方法。

Tests

tests:
  - text: <code>myStr</code>应该具有<code>Hello World</code>的值
    testString: 'assert(myStr === "Hello World", "<code>myStr</code> should have a value of <code>Hello World</code>");'
  - text: 不要更改行上方的代码
    testString: 'assert(/myStr = "Jello World"/.test(code), "Do not change the code above the line");'

Challenge Seed

// Setup
var myStr = "Jello World";

// Only change code below this line

myStr[0] = "H"; // Fix Me

After Test

console.info('after the test');

Solution

// solution required