function square(number) {
return number * number;
}
let cube = function(number) {
return number * number * number;
};
console.log(square(2));
console.log(cube(3));
let historians= ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"];
let i = 0;
while (i < historians.length) {
console.log(historians[i] + " was a historian.");
i+=1;
}
function makeSentences(historians) {
let i = 0,
output = "";
while (i < historians.length) {
output += historians[i] + " was a historian.\n";
i+=1;
}
return output;
}
let h1 = ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"],
h2 = ["Orlando Patterson", "Michel Foucault", "Natalie Zeemon Davis", "Howard Zinn"];
makeSentences(h1);
//makeSentences(h2);
let a = 'just some string',
b = 'some other string';
function scopeExample (anyString) {
let a = 'I set this value inside the function';
return ('inside the function, a="' + a + '", not ' + anyString);
}
// a
// scopeExample(a);
//anyString
a
= "just some string"a
has new valueanyString
is only declared inside the function scope
let a = "global scope a";
console.log(a);
for (i=0; i<6; i++) {
let a = "local scope a on iteration: " + (i + 1) ;
console.log(a)
}
console.log(a);
let
variables in an if/for/while "block"{ .. }
you're in a new scope context!debug by setting a variable to the function output, and using quokka to look at the value:
function returnArray (first, second, third) {
// you can define the array using "new Array ()" or just "[ , , ]"
// don't forget to return it
// return ; // add the value here!
}
let a = returnArray (1, 3,5);
a // quokka will display the value
function robotCleaner () {
let output = "I cleaned your room";
return output;
}
let r = robotCleaner();
r
function robotCleaner () {
let output = `Ha, ha! I have replaced your robot cleaner!
Now your room is even messier! Bwa ha ha ha ha!`
return output;
}
let r = robotCleaner();
r
function greatWriter (name) {
let output = "Margaret Atwood was a great writer."
return output
}
console.log(greatWriter("Margaret Atwood"))
// console.log(greatWriter("Toni Morrison"))
function evenGreaterWriter (name) {
let output = name + " was a great writer."
return output
}
console.log(evenGreaterWriter("Margaret Atwood"))
// console.log(evenGreaterWriter("Toni Morrison"))
let a=["name", 0, "otherinfo"];
console.log(a[2]);
console.log(a.length);
console.log(a[length]);
a.pop;
a[1]
: second element of arraya.length
: a special way of finding the length of any array. Length is a property of a.console.log(a))
: log is a part of console. it takes a parameter , in this case a
a.pop
is not the same as a.pop()