Question: I'm confused about a javascript problem. Please help me on this one. Following link are tests. (I know here are too many tests;( but I'd
I'm confused about a javascript problem. Please help me on this one.




Following link are tests. (I know here are too many tests;( but I'd also be appreciates if you just pass some of them)
https://www.codepile.net/pile/4egBK6mX
/** * This class represents a menu item in a tree menu. The tree menu is a menu that contains a * a hierarchical structure. Each menu item (often called a branch or a node) can have a * number of subitems, * The menu item contains three key information: * * - The name or title of the tree menu item. This is what is displayed on the menu. * - The state of the tree menu item. (checked, unchecked and partial). We will mention this later. Liu Xia, 5 months ago [liuxia] feat: initial commit. * - The direct sub items (children) of the menu item. (If there is none, then the menu item is a *leaf* item). * * * The state of the tree menu item is determined only by its *leaf* item(s). * * * * - If a menu item is a *leaf* item, then when we call 'check() function, it will be in a *checked* state; when we call "uncheck() function, it will be in an *unchecked* state. * - if a menu item contains children, then: - The menu item will be in a *checked* state only if all the *leaf* items are *checked* - The menu item will be in an *unchecked* state only if all the *leaf* items are *unchecked* - The menu item will be in a *partial* state, otherwise. * * Liu Xia, 5 months ago | 1 author (Liu Xia) export class TreeMenuItem { * Create a tree menu item with its name. * @param {string} name The name of the menu item. * @throws The 'name' is not provided, or is a falsy value. */ constructor (name) { if (!name) { throw new Error('The name is a mandatory information.'); } this.name = name; this.children = []; // TODO: you can add additional logic here if you want // /** * Mark current menu item and all its children in a *checked* state. */ check () { // TODO: Please implement the method. // } /** * Mark current menu item and all its children in an *unchecked* state. */ uncheck () { // TODO: Please implement the method. // } /** * Add children to current menu item. Please note that the children's 'state' * should not be modified. However, the state of current menu item and its * ancestors may be affected according to the children's 'state'. *** * @param {...string} menuItem The sub item(s) */ addChildren (...menuItem) { // TODO: Please implement the method. // } /** * Remove a direct children of current menu item by name(s). Please note that * the 'state of current menu item and its ancestors may affected according * to the children's 'state', * * If there is no item with specified 'name', then this method does nothing. * And if there are multiple items with specified `name`, then this method will * remove all of them. * * Please note that this method only remove direct children. It will not * execute recursively. * If current item become a *leaf* item after the removal. Its 'state' should * be *unchecked*. * * @param {...string} names The name of the menu item to delete. */ removeChildren (... names) { // TODO: Please implement the method. // } * Get the state of current item. Please refer to 'ItemState'. */ get state () { // TODO: Please implement the method. // } * This method returns 'true' if current item is a *leaf* item, otherwise it * will return 'false'. */ get isLeaf 0 { return this.children. length === 0; } * Print the tree structure of the item to ease debugging. * @param {number} indentLevel (Optional) The indent space number. */ debugPrint (indentLevel) { const level = indentLevel === undefined ? 0 : indentLevel; const title = (${this.state}) ${this.name}'; const indent = level === 0 ? ":''.repeat (level); return ${indent}${title} + this.children.map(item => item.debugPrint(level + 2)).join(''); } } * This enum represents all possible state of a menu item. * export const ItemState = Object. freeze({ CHECKED: 'checked', UNCHECKED: 'unchecked', PARTIAL: 'partial' }); /** * This class represents a menu item in a tree menu. The tree menu is a menu that contains a * a hierarchical structure. Each menu item (often called a branch or a node) can have a * number of subitems, * The menu item contains three key information: * * - The name or title of the tree menu item. This is what is displayed on the menu. * - The state of the tree menu item. (checked, unchecked and partial). We will mention this later. Liu Xia, 5 months ago [liuxia] feat: initial commit. * - The direct sub items (children) of the menu item. (If there is none, then the menu item is a *leaf* item). * * * The state of the tree menu item is determined only by its *leaf* item(s). * * * * - If a menu item is a *leaf* item, then when we call 'check() function, it will be in a *checked* state; when we call "uncheck() function, it will be in an *unchecked* state. * - if a menu item contains children, then: - The menu item will be in a *checked* state only if all the *leaf* items are *checked* - The menu item will be in an *unchecked* state only if all the *leaf* items are *unchecked* - The menu item will be in a *partial* state, otherwise. * * Liu Xia, 5 months ago | 1 author (Liu Xia) export class TreeMenuItem { * Create a tree menu item with its name. * @param {string} name The name of the menu item. * @throws The 'name' is not provided, or is a falsy value. */ constructor (name) { if (!name) { throw new Error('The name is a mandatory information.'); } this.name = name; this.children = []; // TODO: you can add additional logic here if you want // /** * Mark current menu item and all its children in a *checked* state. */ check () { // TODO: Please implement the method. // } /** * Mark current menu item and all its children in an *unchecked* state. */ uncheck () { // TODO: Please implement the method. // } /** * Add children to current menu item. Please note that the children's 'state' * should not be modified. However, the state of current menu item and its * ancestors may be affected according to the children's 'state'. *** * @param {...string} menuItem The sub item(s) */ addChildren (...menuItem) { // TODO: Please implement the method. // } /** * Remove a direct children of current menu item by name(s). Please note that * the 'state of current menu item and its ancestors may affected according * to the children's 'state', * * If there is no item with specified 'name', then this method does nothing. * And if there are multiple items with specified `name`, then this method will * remove all of them. * * Please note that this method only remove direct children. It will not * execute recursively. * If current item become a *leaf* item after the removal. Its 'state' should * be *unchecked*. * * @param {...string} names The name of the menu item to delete. */ removeChildren (... names) { // TODO: Please implement the method. // } * Get the state of current item. Please refer to 'ItemState'. */ get state () { // TODO: Please implement the method. // } * This method returns 'true' if current item is a *leaf* item, otherwise it * will return 'false'. */ get isLeaf 0 { return this.children. length === 0; } * Print the tree structure of the item to ease debugging. * @param {number} indentLevel (Optional) The indent space number. */ debugPrint (indentLevel) { const level = indentLevel === undefined ? 0 : indentLevel; const title = (${this.state}) ${this.name}'; const indent = level === 0 ? ":''.repeat (level); return ${indent}${title} + this.children.map(item => item.debugPrint(level + 2)).join(''); } } * This enum represents all possible state of a menu item. * export const ItemState = Object. freeze({ CHECKED: 'checked', UNCHECKED: 'unchecked', PARTIAL: 'partial' })
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
