Repository navigation
#297 Destructuring Objects
In JavaScript, you can do basic object assignment like this:
const obj = { one: 1, two: 2 }
let one = obj.one
let two = obj.two
However, with ES6 you can assign the variables in a much more succinct way.Use ES6 object destructuring to assign variables one and two to obj.one and obj.two respectively.
Although you can use let, var, or const for assignment, DO NOT use these in this challenge.
let str = ( /* create assignments here */ = { one : 1, two : 2}).toString()
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/297 Destructuring Objects.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//In JavaScript, you can do basic object assignment like this:
//const obj = { one: 1, two: 2 }
//let one = obj.one
//let two = obj.two
//However, with ES6 you can assign the variables in a much more succinct way.Use ES6 object destructuring to assign variables one and two to obj.one and obj.two respectively.
//Although you can use let, var, or const for assignment, DO NOT use these in this challenge.
//let str = `( /* create assignments here */ = { one : 1, two : 2}).toString()`
const str = `( { one, two } = { one : 1, two : 2}).toString()`
import { assertEquals } from '../testHelper.js';
const assignedValues = {};
({ one: assignedValues.one, two: assignedValues.two } = { one: 1, two: 2 });
const validString = (str) => {
return /\{\s*one\s*,\s*two\s*\}\s*=/.test(str) ? "valid" : "not valid"
}
assertEquals(validString(str), "valid", "You must use the object destructuring to assign variables one and two")
assertEquals(assignedValues.one, 1, "The variable one must equal 1")
assertEquals(assignedValues.two, 2, "The variable two must equal 2")