For all the following exercises create a new .js file. Try to find a proper name for each file or make a small comment about what it does inside for future reference
-
Create a function that takes 3 arguments and returns the sum of the three arguments.
-
Create a function named
colorCarthat receives a color, and prints out, "a red car" for example. (Hint: put it in your file and run it like before.) -
Create an object and a function that takes the object as a parameter and prints out all of its names and values.
-
Create a function named
vehicleTypethat receives a color, and a code, 1 for car, 2 for motorbike. And prints "a blue motorbike" for example when called asvehicleType("blue", 2) -
Can you write the following without the
ifstatement, but with just as a single line withconsole.log(...);?
if (3 == 3) {
console.log("true")
} else {
console.log("false")
}-
Create a function called
vehicle, like before, but takes another parameter called age, so thatvehicle("blue", 1, 5)prints "a blue used car" -
Make a list of vehicles, you can add
"motorbike","caravan","bike", or more. -
How do you get the third element from that list?
-
Change the function
vehicleto use the list of question 5. So thatvehicle("green", 3, 1)prints "a green new caravan". -
Use the list of vehicles to write an advertisment. So that it prints something like:
"Amazing Joe's Garage, we service cars, motorbikes, caravans and bikes.". (Hint: use aforloop.) -
What if you add one more vehicle to the list, can you have that added to the advertisement without changing the code for question 8?
-
Create a function that takes two objects as parameters and compares them. You will actually need to write two functions — one that compares with
==and one that compares with===. Remember that objects can have objects inside of them so you'll need to find a way to compare every element of every object (types and values). For example:var obj1 = { a: 1, b: 'this is the letter b', c: { foo: 'what is a foo anyway', bar: [1,2,3,4] } } var obj2 = { a: '1', b: 'this is the letter b', c: { foo: 'what is a foo anyway', bar: [1,2,3,4] } }
In our example we'll say that
obj1 == obj2istrueandobj1 === obj2isfalse. Make sure you can see why before you write any code!Note: give this exercise your best shot but don’t spend more than, say, one hour on it.
-
We saw that we can pass functions as arguments to other functions. Your task is to write a function that takes another function as an argument and runs it.
function foo(func) { // What to do here? } function bar() { console.log('Hello, I am bar!'); } foo(bar);
Write some code to test two arrays for equality using
==and===. Test the following:var x = [1,2,3]; var y = [1,2,3]; var z = y;
What do you think will happen with
x == y,x === yandz == yandz == x? Prove it!Don't cheat! Seriously - try it first.
Check out this Fiddle. You need to open your browser’s Developer Tools to see the console output. Press the Run button in the upper right corner to run the code.
More insights from this Stack Overflow question.
-
We did the following example in class:
var o1 = { foo: 'bar' }; var o2 = { foo: 'bar' }; var o3 = o2;
Show that changing
o2changeso3(or not) and changingo2changeso3o1changeso3(or not).Does the order that you assign (
o3 = o2oro2 = o3) matter? {Jim Cramer: ???}
Have a look at The Secret Life of JavaScript Primitives
‘Coerce' means to try to change - so coercing
var x = '6'to number means trying to change the type to number temporarily.