Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New index.js for tiny js task #258

Merged
merged 8 commits into from
Aug 25, 2022
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions submissions/Levi123/tiny-JS/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* 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/Levi123/a-tiny-JS-world
Web app: _put project's github pages URL here_
*/

// ======== OBJECTS DEFINITIONS ========
// Define your objects here
const dog = {
species: 'dog',
name: 'Rich',
gender: 'male',
legs: 4,
hands: 0,
saying: 'Hav-hav!',
friends: ['Bonya'],
}

const cat = {
species: 'cat',
name: 'Bonya',
gender: 'female',
legs: 4,
hands: 0,
saying: 'Mr-mr-mr',
friends: ['Rich', 'Catwoman'],
}

const female = {
species: 'human',
name: 'Olya',
gender: 'female',
legs: 4,
hands: 4,
saying: "Glory to Ukraine",
friends: ['Yura', 'Rich'],
}

const male = {
species: 'human',
name: 'Yura',
gender: 'male',
legs: 4,
hands: 4,
saying: "Glory to Ukraine",
friends: ['Olya', 'Bonya'],
}

const catWoman = {
species: 'human',
name: 'Catwoman',
gender: 'female',
legs: 4,
hands: 4,
saying: cat.saying,
friends: ['Bonya']
}
// ======== OUTPUT ========
/* Use print(message) for output.
Default tag for message is <pre>. Use print(message,'div') to change containing element tag.

Message can contain HTML markup. You may also tweak index.html and/or styles.css.
However, please, REFRAIN from improving visuals at least until your code is reviewed
so code reviewers might focus on a single file that is index.js.
*/

const prop = ['species', 'name', 'gender', 'legs', 'hands', 'saying', 'friends']
const allCharactersArray = [catWoman, male, female, cat, dog]

// function printObjectNew(personToPrint){
// const propToPrint = [];
// prop.forEach((propOfObj) => {
// propToPrint.push(personToPrint[propOfObj]);
// })
// let newStr = propToPrint.join('; ');
// print(newStr);
// }

function arrayToString(propToPrint){
let newStr = propToPrint.join('; ');
console.log(propToPrint);
print(newStr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function promises to convert an array to a string but it also prints.
Also remove calls to console before submitting code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

function arrayOfPropToPrint(personToPrint){
const propToPrint = [];
prop.forEach((propOfObj) => {
propToPrint.push(personToPrint[propOfObj]);
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A perfect use case for Array#map method.
Also prop is a global variable. Do not use global variables where there is no need, esp. if it is used in a specific function and only there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrayToString(propToPrint);
}

function printAllObjects(arrayAllObjects){
arrayAllObjects.forEach((characters) => {
arrayOfPropToPrint(characters);
})
}

printAllObjects(allCharactersArray);

// printObjectNew(catWoman);
// printObjectNew(male);
// printObjectNew(female);
// printObjectNew(cat);
// printObjectNew(dog);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Make your code editor to ensure you always have an empty line at the end of file. There must be a setting. Check this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Done