Make sure to test all your answers in the console.
var a = [Math.random(), Math.random(), Math.random()]
.
a
. You should use slice
to do it. var b = ["hi", "there", "you"]
. Which of the following would result in the string "hi,there,you"
? Circle all that are correct.
b.join()
.b.join(,)
.b.join(",")
.b.join(',')
.b.join("hi,there,you")
.var s = 0, i;
for (i = 0; i < a.length; i += 1) {
s = ... // Fill this in
}
console.log(s);
var o = { foo: 23, bar: 5 }
.
o.foo
o[foo]
o[("foo")]
o."foo"
o.baz
return?
undefined
null
5
o.bar = undefined
, how many keys does o
have?
1
2
3
o.bar = { baz: 2 }
, what would o["bar.baz"]
return?
2
undefined
null
For this question we start with an array of objects from a shopping cart:
var cart = [
{ item: "shirt", quantity: 1 , price: 10 },
{ item: "shoes", quantity: 3 , price: 5 },
{ item: "pens" , quantity: 10, price: 2 }
];
The following code is meant to compute the total cost of everything in the cart. The prices are per item, so they need to be multiplied by the quantities. Fill in the body of the loop:
var total = 0, i, obj;
for (i = 0; i < cart.length; i += 1) {
// Fill this in
}
console.log(total);
We decide that we want to buy two more shoes, so we want to change the information in the cart to have the adjusted quantities. Write an expression that would make that happen.