From 7972a8d0578010dc729321fe73342528a556800b Mon Sep 17 00:00:00 2001 From: Bohdan Mukha Date: Tue, 16 Aug 2022 22:10:51 +0300 Subject: [PATCH 1/3] add tiny-js-world homework --- submissions/bmukha/tiny-js-world/index.js | 82 +++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 submissions/bmukha/tiny-js-world/index.js diff --git a/submissions/bmukha/tiny-js-world/index.js b/submissions/bmukha/tiny-js-world/index.js new file mode 100644 index 0000000000..72cdd555a4 --- /dev/null +++ b/submissions/bmukha/tiny-js-world/index.js @@ -0,0 +1,82 @@ +/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details + Complete the below for code reviewers' convenience: + + Code repository: https://github.com/bmukha/a-tiny-JS-world + Web app: https://bmukha.github.io/a-tiny-JS-world/ + */ + +const dog = { + species: 'dog', + name: 'Beethoven', + gender: 'male', + legs: 4, + hands: 0, + saying: 'woof', + friends: ['Clyde'], +}; + +const cat = { + species: 'cat', + name: 'Grizabella', + gender: 'female', + legs: 4, + hands: 0, + saying: 'meow', + friends: ['Bonnie'], +}; + +const man = { + species: 'human', + name: 'Clyde', + gender: 'male', + legs: 2, + hands: 2, + saying: 'Get rich or die trying!', + friends: ['Bonnie', 'Beethoven'], +}; + +const woman = { + species: 'human', + name: 'Bonnie', + gender: 'female', + legs: 2, + hands: 2, + saying: 'I have the right to not answer a questions!', + friends: ['Clyde', 'Grizabella'], +}; + +const catwoman = { + species: 'human', + name: 'Selina', + gender: 'female', + legs: 2, + hands: 2, + saying: cat.saying, + friends: [], +}; + +const characters = [dog, cat, man, woman, catwoman]; + +const props = [ + 'species', + 'name', + 'gender', + 'legs', + 'hands', + 'saying', + 'friends', +]; + +characters.forEach((character) => { + let representation = ''; + for (let i = 0; i < props.length; i++) { + if (props[i] === 'hands' && character[props[i]] === 0) { + continue; + } else if (i < props.length - 1) { + representation += `${character[props[i]]}; `; + } else { + representation += `${character[props[i]].join(', ')}`; + } + } + print(representation); +}); From 7c0cc130408d0e3ff528561b0bbd496c43d32798 Mon Sep 17 00:00:00 2001 From: Bohdan Mukha Date: Wed, 17 Aug 2022 13:14:02 +0300 Subject: [PATCH 2/3] refactored according to mentor's suggestions --- submissions/bmukha/tiny-js-world/index.js | 25 +++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/submissions/bmukha/tiny-js-world/index.js b/submissions/bmukha/tiny-js-world/index.js index 72cdd555a4..64e62a47a9 100644 --- a/submissions/bmukha/tiny-js-world/index.js +++ b/submissions/bmukha/tiny-js-world/index.js @@ -68,15 +68,18 @@ const props = [ ]; characters.forEach((character) => { - let representation = ''; - for (let i = 0; i < props.length; i++) { - if (props[i] === 'hands' && character[props[i]] === 0) { - continue; - } else if (i < props.length - 1) { - representation += `${character[props[i]]}; `; - } else { - representation += `${character[props[i]].join(', ')}`; - } - } - print(representation); + const noZeroHandsOrFriends = props.filter( + (prop) => + !( + (prop === 'hands' && character[prop] === 0) || + (Array.isArray(character[prop]) && character[prop].length === 0) + ) + ); + const values = noZeroHandsOrFriends.map((prop) => + Array.isArray(character[prop]) + ? character[prop].join(', ') + : character[prop] + ); + + print(values.join('; ')); }); From f251e83be160a5eae277020cfbe9d93ca163db5c Mon Sep 17 00:00:00 2001 From: Bohdan Mukha Date: Thu, 18 Aug 2022 16:42:26 +0300 Subject: [PATCH 3/3] refactored forEach --- submissions/bmukha/tiny-js-world/index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/submissions/bmukha/tiny-js-world/index.js b/submissions/bmukha/tiny-js-world/index.js index 64e62a47a9..193a699d63 100644 --- a/submissions/bmukha/tiny-js-world/index.js +++ b/submissions/bmukha/tiny-js-world/index.js @@ -68,18 +68,14 @@ const props = [ ]; characters.forEach((character) => { - const noZeroHandsOrFriends = props.filter( - (prop) => - !( - (prop === 'hands' && character[prop] === 0) || - (Array.isArray(character[prop]) && character[prop].length === 0) + print( + props + .map((prop) => + Array.isArray(character[prop]) + ? character[prop].join(', ') + : character[prop] ) + .filter((value) => Boolean(value)) + .join('; ') ); - const values = noZeroHandsOrFriends.map((prop) => - Array.isArray(character[prop]) - ? character[prop].join(', ') - : character[prop] - ); - - print(values.join('; ')); });