Kristofer Koishigawa b3213fc892 fix(i18n): chinese test suite (#38220)
* fix: Chinese test suite

Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working.

* fix: ran script, updated testStrings and solutions
2020-03-03 18:49:47 +05:30

2.8 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7b7d367417b2b2512b1f Modify an Array Stored in an Object 1 修改存储在对象中的数组

Description

现在您已经看到了JavaScript对象的所有基本操作。您可以添加修改和删除键值对检查键是否存在并迭代对象中的所有键。随着您继续学习JavaScript您将看到更多功能的对象应用程序。此外课程后面的可选高级数据结构课程还涵盖ES6 MapSet对象这两个对象与普通对象类似但提供了一些附加功能。既然您已经学习了数组和对象的基础知识那么您已经准备好开始使用JavaScript解决更复杂的问题了

Instructions

看看我们在代码编辑器中提供的对象。 user对象包含三个键。 data键包含五个键,其中一个键包含一组friends 。从这里,您可以看到灵活的对象如何作为数据结构。我们已经开始编写一个函数addFriend 。完成编写它以便它获取user对象并将friend参数的名称添加到存储在user.data.friends中的数组并返回该数组。

Tests

tests:
  - text: <code>user</code>对象具有<code>name</code>  <code>age</code>和<code>data</code>键
    testString: assert('name' in user && 'age' in user && 'data' in user);
  - text: <code>addFriend</code>函数接受<code>user</code>对象和<code>friend</code>字符串作为参数,并将朋友添加到<code>user</code>对象中的<code>friends</code>数组
    testString: assert((function() { let L1 = user.data.friends.length; addFriend(user, 'Sean'); let L2 = user.data.friends.length; return (L2 === L1 + 1); })());
  - text: '<code>addFriend(user, &quot;Pete&quot;)</code>应该返回<code>[&quot;Sam&quot;, &quot;Kira&quot;, &quot;Tomo&quot;, &quot;Pete&quot;]</code>'
    testString: assert.deepEqual((function() { delete user.data.friends; user.data.friends = ['Sam', 'Kira', 'Tomo']; return addFriend(user, 'Pete') })(), ['Sam', 'Kira', 'Tomo', 'Pete']);

Challenge Seed

let user = {
  name: 'Kenneth',
  age: 28,
  data: {
    username: 'kennethCodesAllDay',
    joinDate: 'March 26, 2016',
    organization: 'freeCodeCamp',
    friends: [
      'Sam',
      'Kira',
      'Tomo'
    ],
    location: {
      city: 'San Francisco',
      state: 'CA',
      country: 'USA'
    }
  }
};

function addFriend(userObj, friend) {
  // change code below this line

  // change code above this line
}

console.log(addFriend(user, 'Pete'));

Solution

// solution required