-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
71 lines (58 loc) · 2.32 KB
/
Copy pathfunctions.js
File metadata and controls
71 lines (58 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const men =[{name:'Socrates', mortal: true},{name:'Évripedes',mortal: true},{name: 'Achiles',mortal: false}];
//Check if there is a name we search inside this array of objects
let howManyMen = men.length;
function tellMeIsHeInTheList(hisName) {
let listObject={};
let listStatus = false;
let mortalStatus;
// first we chech if the given name is in out list
for (let i = 0; i < howManyMen; i++) {
if (men[i].name === hisName) {
listObject = men[i];
listStatus = true;
}
}
// if the given name is in our list then we check his mortal status
if(listStatus){
mortalStatus= listObject.mortal;
if(mortalStatus){
console.log("Yes he is mortal");
}else{
console.log("Yes he is importal");
}
}else{
console.log("Sorry we haven't find this person inside our list please try again")
}
};
// here we have 2 names with different mortal status
tellMeIsHeInTheList('Socrates');
tellMeIsHeInTheList('Achiles');
tellMeIsHeInTheList('Giannis');
//let's do the next bonus project regarding the cakes and if they have chocolate
const myCakes =
[
{ name: "vanilla dream", hasChocolate: false, flavor: "Madadascar vanilla pandesia" },
{ name: "Strawbery full", hasChocolate: true, flavor: "the classic combination of strawberry with chocolate" }
]
// here we are about to use the filter method to get the object with the requested name
let isItInList;
function SearchCakes(cakeName) {
let theSelectedCake = myCakes.filter(isIntheList => { return isIntheList.name === cakeName });
// Here we check if there is an object with the requested name
if (theSelectedCake.length > 0) {
// Now we check if the selected cake has chocolate
let SelectedItem = theSelectedCake[0]
if (SelectedItem.hasChocolate) {
console.log("the cake has chocolate and the flavor is " + SelectedItem.flavor);
}
else {
console.log("the selected cake doesn't have chocolate and the flavor is " + SelectedItem.flavor);
}
} else {
console.log("there is no cake with this name");
}
}
//here we call the function
var myResults = SearchCakes('vanilla dream');
var myResults = SearchCakes('Strawbery full');
var myResults = SearchCakes('no name');