JSFiddle - React, Tailwind, and code Playground

by hito

JavaScript

// These exercises are meant to get you familiar with some basic concepts in regards to JavaScript object, Arrays, and loop structures. Questions will be spelled out in the comments below. Take a look at the questions as they appear in code. Most of the question will ask you to log things to the console, which can be done through the console.log( ) method.

// For example:
var test = "foo";

// 1) Log the value of the test variable to the console.

// BEGIN ANSWER
console.log( test );
// END ANSWER

// once this fiddle is run you'll see the console.log statement appear in the result pane to the right.

// And now for the exercises.

// Exercise 1) Initialize an array and then fill it with 30 values. Each value should be a multiple of 3, starting with 3. Log to the console the 15th value of the array. Log to the console the length of the array.

// BEGIN Exercise 1 Answer
for (var i=0, arr=[];i<30;i++){
     arr[i] = 3*i+3;  
}
console.log(arr[14]);
console.log(arr);
console.log(arr.length);
// END Exercise 1 Answer

// Exercise 2) Create an object with properties of "foo" and "bar-baz", repectively. The values can be whatever you'd like (be creative!). Log both of these properties to the console.

// BEGIN Exercise 2 Answer
var object = {
    "foo" : "Come one",
    "bar-baz" : "it's easy"
};
console.log (object.foo);
console.log (object["bar-baz"]);
//optionnal 
console.log (object);
for (var each in object) {
    console.log (each + ": " + object[each]);
}

// END Exercise 2 Answer

// Exercise 3) Use the object created in example 3. Augment a new property on the object with the key "qux". The value can be whatever you'd like. Log this new property to the console.

// BEGIN Exercise 3 Answer

object.qux = "New property";
console.log (object.qux);
//optionnal
for (var newProp in object);
console.log(newProp + ": " + object[newProp]);


// END Exercise 3 Answer

// Exercise 4) Use the object from exercise 4. Augment the object with the properties "bar1", "bar2",...