let myFruit="orange",
mySandwich = "just some bread!",
myBar="weird vegan bar",
myCookie="ginger",
myCandy="pulparindo",
mySnacks=["cliff bar", "cliff bar", "kind bar"];
function eat (food) {
console.log("Umm, that was a delicious " + food + ".");
}
eat (myFruit);
//eat (myBar);
//eat (myCookie);
//eat (myCandy);
let myFruit="orange",
mySandwich = "just some bread!",
myBar="weird vegan bar",
myCookie="ginger",
myCandy="pulparindo",
mySnacks=["cliff bar", "cliff bar", "kind bar"];
let myLunch = {};
let myFruit="orange",
mySandwich = "just some bread!",
myBar="weird vegan bar",
myCookie="ginger",
myCandy="pulparindo",
mySnacks=["cliff bar", "cliff bar", "kind bar"];
let myLunch = {
fruit: "orange",
sandwich: mySandwich
};
myLunch["snacks"]=mySnacks;
myLunch;
let myFruit="orange",
mySandwich = "just some bread!",
myBar="weird vegan bar",
myCookie="ginger",
myCandy="pulparindo",
mySnacks=["cliff bar", "cliff bar", "kind bar"];
let myLunch = {
fruit: "orange",
sandwich: mySandwich
};
myLunch["snacks"]=mySnacks;
function eatLunch (someLunch) {
let output = "";
output += "I'll start with my " + someLunch["fruit"] + "\n";
output += "Next I want to eat " + someLunch.sandwich + "\n";
for (snack of someLunch.snacks) {
output += " I love my " + snack + "\n";
}
return output;
}
eatLunch(myLunch);
let myLunch = {
fruit: "orange",
sandwich: "just some bred!",
bar: "weird vegan bar",
snacks: ["cliff bar", "cliff bar", "kind bar"],
dessert: {cookie: "ginger", candy: "pulparindo"}
};
console.log("I'm finally ready for my " + myLunch.dessert.cookie);
function eatLunch (someLunch) {
let output = "";
output += "I'll start with my " + someLunch["fruit"] + "\n";
output += "Next I want to eat " + someLunch.sandwich + "\n";
for (snack of someLunch.snacks) {
output += " I love my " + snack + "\n";
}
return output;
}
let myLunch = {
fruit: "orange",
sandwich: "just some bred!",
bar: "weird vegan bar",
snacks: ["cliff bar", "cliff bar", "kind bar"],
dessert: {cookie: "ginger", candy: "pulparindo"},
eatMe: function() {
let output = "";
output += "I'll start with my " + this["fruit"] + "\n";
output += "Next I want to eat " + this.sandwich + "\n";
for (snack of this.snacks) {
output += " I love my " + snack + "\n";
}
return output;
}
};
myLunch.eatMe;
myLunch.eatMe();
let greatWar = {
name: "The First World War",
start: 1914,
end: 1918,
badGuys: ["Germany", "Austria-Hungary", "Ottoman Empire"],
goodGuys: ["All our Friends"],
scale: "Catastrophe"
}
// console.log(greatWar["name"] + " lasted for " + (greatWar.end - greatWar.start) + " years.");
let greatWar = {
name: "The First World War",
start: 1914,
end: 1918,
badGuys: ["Germany", "Austria-Hungary", "Ottoman Empire"],
goodGuys: ["All our Friends"],
scale: "Catastrophe",
battles: [
{ name: "The Battle of Vimy Ridge",
year: 1915,
casualties: 34000
}]
}
console.log(greatWar["name"] + " lasted for " + (greatWar.end - greatWar.start) +
" years.");
console.log(greatWar.battles[0].name + " was ferocious and horrific. There were " +
greatWar.battles[0].casualties + " casualties.");
let goodGuys = ["CA", "UK", "US"];
let badGuys=["DE", "AH"];
let people = [
{name: "Kaiser Wilhelm",
nat: "DE"},
{name:"Winston Churchill",
nat: "UK"}];
for (person of people) {
if (badGuys.includes(person.nat)) {
//console.log(person.name + ": BOOOOOOOOOOOO!!!");
}
};
let goodGuys = ["CA", "UK", "US"];
let badGuys=["DE", "AH"];
let people = [
{name: "Kaiser Wilhelm",
nat: "DE"},
{name:"Winston Churchill",
nat: "UK"}];
for (person of people) {
if (badGuys.includes(person.nat)) {
console.log(person.name + ": BOOOOOOOOOOOO!!!");
} else {
console.log(person.name + ": YAAAAYYY!!!");
}
};
Take a look at this the code snippets repo
Now consider the following javascript array of objects (here you can see how similar JSON is to regular javascript objects)
orgs = [
{"name": "African National Congress",
"founded": 1912,
"link": "https://en.wikipedia.org/wiki/African_National_Congress"
},
{"name": "Pan Africanist Congress",
"founded": 1959,
"link": "https://en.wikipedia.org/wiki/Pan_Africanist_Congress_of_Azania"
},
{"name": "South African Communist Party",
"founded": 1921,
"link": "https://en.wikipedia.org/wiki/South_African_Communist_Party"
},
{
"name": "Black Sash",
"founded": 1955,
"link": "https://en.wikipedia.org/wiki/Black_Sash"
},
{
"name": "South African Students' Organization",
"founded": 1968,
"link": "https://en.wikipedia.org/wiki/South_African_Students%27_Organisation"
}
]
orgs
Our objective is to read this variable and use it to build sentences about anti-apartheid organizations in South Africa.
function describeOrg (org) {
return org.name + ' was founded in ' + org.founded + '.';
}
console.log(describeOrg(orgs[0]));
function describeOrg (org) {
return org.name + ' was founded in ' + org.founded + '.';
}
for (let o of orgs) {
console.log(describeOrg(o));
}