Data Structures (Arrays and Objects) - Reading Assignment

  1. Variable types such as strings and integers only store single points of data. In this scenario , we need to store multiple sets of data.

  2. Arrays

  3. Length of an array and characters in uppercase

  4. Null and undefined

  5. array.length or array[“length”]

  6. Performs a function on the value of an array such as .push

  7. Collection of properties

  8. They can change instead of being immutable

  9. A variable with braces and properties inside them

  10. The values can be changed

SECOND PART

  1. Strings are immutable

  2. Putting three dots before the parameter

  3. Serialisation is for converting data stored in memory into a description. JSON is a form of serialisation

  4. If you want to save data in a file or send it to another computer over the network then you must convert the tangles of memory addresses to a description that you can send. JSON is the solution for this.

  5. Each value needs to be surrounded by double quotes.

1 Like

Answer:

  1. In the Weresquirrel chapter, we have a problem of storing sequences of values and also different types of them. A variable type string can hold one line of string. Same with the integer type, but it can hold only one string value.
  2. Variable type “Object”.
  3. Properties in JavaScript are object’s names that stores values. They are represented by property name, colon “:” and property value. Almost every value in Javascript has a property. In the array, elements are the properties, and the numbers are their names or keys.
  4. Null and Undefined does not have properties.
  5. One way to use dot. Example: “value.name”. The other way is to use brackets. Example: “value[name]”.
  6. Methods are actions that you can perform on the values. Methods are properties that contain functions.
  7. Objects are a way of storing many different values of a different type. Objects can contain properties and methods.
  8. All the other value types can store only one type of value. On other hand objects can store any type of value in one binding.
  9. You declare a name followed by equal sign, then open a Curly Brackets and declare all the properties and methods inside of them.
  10. Object’s properties and methods can be changed with a code from outside. Different objects can grasp the same location of values. If you change the values of a variable, the change is reflected in all other references to that variable. You have to be careful not to make unwanted changes in an object when working with its values.
1 Like
  1. That a variable type is needed to store multiple values.
  2. An array
    3.Properties are the values that are defined inside an object.
    4.undefined and null.
    5.using . or [ ]
  3. Properties that contain functions
    7.They are data structures that contain many properties.
    8.Objects can store different kinds of values in same variable.
    9.By using { }
  4. You can define on object and afterwards still be able to remove a property or method and define again.

Second part

  1. They are immutable.
  2. It allows us to represent any number of arguments as an array.
  3. It re-formats a program to give it a flat description.
  4. Javascript Object Notation which is a storage format.
    6 JSON only allows data and names with " " surrounding them.
1 Like

Answer:

  1. We can’t add new properties to a string variable because it is immutable. We can try adding new properties to a string value, but it doesn’t store them.
  2. Rest parameters help us to create an array for a function, for which we don’t know an exact amount of arguments. So when we pass arguments of different quantities, it creates an array for each of them.
  3. Serialisation is a process of converting the data into a flat description, a file that can be easily transmitted to another computer. It is used for data storage and transmission.
  4. JSON is JavaScript Object Notation, it is a format of serialization for data storage and transmission on the Web, and it even used by other languages.
  5. JSON file stores only data. It doesn’t allow function calls, bindings, computations, or comments. The JSON file contains only properties and their values. Double-quotes must surround all properties.
1 Like

First part:

  1. Jacque needs to store data capable of holding multiple values and types.
  2. The variables he can use are called arrays.
  3. Properties are expressions that acces a property of a value.
  4. Null and undefined have no properties and if you try to acces them you get an error.
  5. We can acces properties with dot (value.x) and with square brackets like value[x].
  6. Methods are properties that contain functions.
  7. An object is a data structure with a collection of properties. An object is created by using curly braces as an expression.
  8. An object can contain any type of values like boolean, integer, string, array.
  9. For easy understanding i would define an object like an octopus with any number of tentacles, each of which has a name tattoed on it.
  10. Mutability means that the values of an object can be modified.

Second part:

  1. We can’t add new properties to a string variable because strings are not objects.
  2. The rest parameter is bound to an array containing all further arguments. It can be recognized by having three dots before his name.
  3. Serialisation means to convert data in a flat description. It is useful when you want to save data in a file or send it to another computer over network.
  4. JSON is a popular serialisation widely used as a data storage and communication format on the web.
  5. In JSON all property names have to be surrounded by double quotes and only simple data expressions are allowed, no function calls, no bindings or anything that involves the actual computation.
1 Like
  1. Jacques is trying to find out which actions during a certain day triggers a certain event.
  2. Objects can store arbitrary collections of properties.
  3. Properties are the values associated with a JS object.
  4. null and undefined do not have properties.
  5. We can use either a dot or square brackets. value.x will get the property of value named ‘x’. value[x] will evaluate ‘x’ and uses the result converted to a string, as the property name.
  6. Properties that contain functions are called methods of the value they belong to.
  7. Objects are arbitrary collections of properties.
  8. The values of objects can be modified. Other types of bindings are immutable.
  9. We can define an object by using braces. Inside the braces is a list of properties separated by commas. Each property has a name followed by a colon and a value.
  10. JS objects are mutable. Their properties can have a different content at different times while the program runs.
1 Like
  1. String values are immutable, so adding properties is prevented by definition in JS.
  2. Rest parameters are used for functions to accept any number of parameters from arrays. They are expressed by three dots before the name of the array.
  3. Serialization is the conversion of data into a flat description, which allows for data to be saved in a file or send to another computer.
  4. JSON is JS Object Notation. It is widely used for data storage and communication on the the web even for other languages than JS.
  5. In JSON, all property names have to be surrounded by double quotes, and only simple data expressions are allowed - no function calls, bindings, comments or computations.
1 Like

Part One:

  1. The weresquirrel was introducing the problem of using a data structure that can store multiple values.
  2. An array solves the problem of storing multiple variables.
  3. Property is a string, number, or boolean.
  4. Null and undefined are two values that don’t have properties.
  5. Two ways to access properties in value are to use a dot or square bracket like: value.x or value[x]
  6. A method is a function that is attached to a property it is usually written like: variable.method().
  7. An object is a list that can hold lists that contain different kinds of properties such as numbers, booleans, and strings.
  8. Objects are used to store an arbitrary list of properties such as numbers and booleans or numbers and strings.
  9. An object is created using curly braces. Properties are separated by commas, each property has a name followed by a colon and value.
  10. Javascript objects are muttable and you can change the properties of them.

Part Two:

  1. Strings, numbers, booleans are immutable objects and properties cannot be changed or added.
  2. A rest parameter is can be used in a function to allow easy handling of any unknown number of items in an array it typicaly uses a three-dot notation.
  3. N/A
  4. Serialisation is the process of taking data and converting it to a simple format that only contains data and no links or computations. It is used to store and send information over the internet.
  5. JSON is used to serialize, store, and send data over the internet using JavaScript and other popular programming languages.
  6. In JSON all property names need to be surrounded by double quotes and only simple data allowed. No functions, bindings or computations allowed.
1 Like

Answers

First part:

  1. With variable types, such as strings or integers, it’s impossibile to group values and build complex structures.

  2. For storing multiple values we can use objects and arrays.

  3. Properties in JS define some characteristic about values in a data structure (for instance length of a string and index of array’s value).

  4. Null and undefined have no properties.

  5. We can access properties in a value using a dot and the literal name of the property (i.e. array.length) or using the square brackets (i.e. array[“lenght”]).

  6. Methods are properties that hold function values.

  7. Objects are data structures that contain an arbitrary collections of properties.

  8. Objects are very useful to hold different datatypes.

  9. To create an object we can use braces as an expression. Inside the braces, there is a list of properties separated by commas. Each property has a name followed by a colon and a value.

  10. A single object can change their properties different times. This is completely different to other datatype, such as strings, numbers and booleans, which will maintain unchanged their value during times.

Second Part

  1. A string is immutable, it’s not an object.

  2. Rest parameters are useful for a function to accept any number of arguments. To write this function, you should put three dots before the function’s last parameter, such as function max(…numbers).

  3. Thanks

  4. Serialisation is the process of converting data stored in memory into a flat description. It’s useful if we want to save data in a file for later or send it to another computer over the network.

  5. JSON stands for JavaScript Object Notation, used as a data storage and communication format on the Web.

  6. JSON looks very similar to JS’s way of writing arrays and objects, with a few restrictions. All property names have to be surrounded by double quotes and only simple data expressions are allowed, no functions calls. Comments are not allowed in JSON.

2 Likes

Part One

1. Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as strings or integers?

The ability to efficiently store multiple pieces of data in one place.

2. What variable type can be used in order to solve the problem of storing multiple values?

Arrays.

3. What are properties in Javascript?

They are characteristics, e.g. StringNameHere.length will give you the length of the string.

4 . Which values do not have properties?

Null and undefined. If you try to access properties of one of these, you get an error.

5. How can we access properties in a value (two ways)?

With a dot and with square brackets.

6. What are methods?

Properties that contain functions are called methods, e.g. the push method will add values to the end of an array.

7. What are objects?

Objects can be described as being like octopuses with any number of tentacles, each of which has a name tattooed on it.

8. What problem do objects solve that cannot be solved with other value types we’ve learned so far (such as integer, string, array, boolean etc)?

They allow us to create collections of data and group these into a single variable, e.g. we can store a list of activities and combine this with a boolean of whether Jacques metamorphosed into a squirrel or not on that day and put all of this into a single variable called “day1”.

9. How do you define an object?

Objects are defined as variables with the collection of data listed within { } , e.g. var day1 = {squirrel: false, events: [“work”, “touched tree”, “pizza”, “peanuts”] };

10. What can you say about the mutability of Javascript objects?

Numbers, strings, and boolean cannot be changed – they are immutable. However, Javascript objects can be changed and are therefore mutable / they possess mutability. For example, entering object1.value = 15; into the console will change the value of object1 to 15 or any other number you wish.

Part Two

  1. Why can’t you add new properties to a string variable?

Strings, numbers, and boolean are not objects, and though the language doesn’t complain if you try to set new properties to them, it doesn’t actually store those properties. This is because they are immutable and cannot be changed.

  1. What are rest parameters?

The rest parameter syntax allows us to gather any number of arguments into an array.

3. (Feel free to skip the sub-chapter of Math object and Destructing)

4. What is serialisation and what is a use case of serialisation of data?

To serialise data means to convert it into a flat description for storage or to be sent via communication.

5. What is JSON?

JSON stands for JavaScript Object Notation. JSON is a serialisation format used for data storage and communication, even in languages other than JavaScript.

6. What are the differences between JSON and the way programmers write objects in plain Javascript?

JSON looks similar to the way arrays and objects are written in JavaScript but contains a number of restrictions, e.g. all property names must be surrounded by double quotes and nothing is allowed that requires actual computation, e.g. no function calls. Comments are not allowed in JSON either.

1 Like
  1. The problem is how to structure the information of the daily events in a log file.
  2. Fortunately, JavaScript provides a data type specifically for storing sequences
    of values. It is called an [array] and is written as a list of values between square
    brackets, separated by commas.
  3. Properties: are expressions that access a property of some value. Almost all JavaScript values have properties except null and undefined.
  4. null and undefined values do not have properties.
  5. The two main ways to access properties in JavaScript are with a dot and with
    square brackets e.g. Both value.x and value[x] access a property on value.
  6. Methods are properties that contain functions that belong to the values e.g. console.log(doh.toUpperCase()); Here method is toUpperCase()
  7. Objects are a collection of properties separated by commas and wrapped by braces. Each property has a name followed by a colon and a value. e.g. object: descriptions
    let descriptions = {
    work: “Went to work”,
    “touched tree”: “Touched a tree”
    }; properties are work and “touched tree”.
    Properties whose names aren’t valid binding names or valid numbers have to be quoted. e.g “touched tree”.
  8. Object solved the issue of collecting a list of properties which not only consist of numbers or strings but also lists of activities and Boolean values.
  9. Object may be defined as an unordered collection of related data, of primitive or reference types, in the form of “key: value” pairs. These keys can be variables or functions and are called properties and methods, respectively, in the context of an object.
  10. A mutable object is an object whose state can be modified after it is created. Examples of native JavaScript values that are immutable are numbers and strings. Examples of native JavaScript values that are mutable include objects, arrays, functions, classes, sets, and maps.

SECOND PART:

  1. string variables are called primitive values and they cannot have properties
  2. Rest parameter is an improved way to handle function parameter, allowing us to more easily handle various input as parameters in a function.
  3. Serialization is a mechanism for storing an object’s states into a persistent storage like disk files, databases, or sending object’s states over the network.
  4. JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page.
  5. All object keys in JSON must be strings. In Javascript object keys can be strings or numbers. In JSON a function cannot be defined as a value of an object (since this is Javascript specific). In Javascript this is completely legal.
1 Like

FIRST PART

  1. They can only contain one single type of data. We need multiple values in the same container.

  2. Arrays

  3. The characteristics of the values in data structures.

  4. null and undefined

  • With a dot and the property name, like value.length
  • Using brackets, like value[“length”]
  1. Properties holding function values. Special kinds of functions that only work on the value they belong to.

  2. Data structures containing a collection of properties.

  3. They are able to hold an infinite amount of datatypes.

  4. Like a variable, the value is a list in braces.

  5. It means that the values within can be changed.

SECOND PART

  1. A string is a primitive type, it can’t have more properties than being language-based.

  2. Parameters that accept any number of arguments.

  3. Serialization is the process of converting data structures or a object state into a format that can be stored. It is used to save data in a file or send it to another computer over the network.

  4. JavaScript Object Notation, a serialization format.

  5. All property names need to be surrounded in double quotes. Only simple data expressions are allowed.

1 Like
  1. Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as strings or integers?
  • Storing multiple string values per variable and changing data per variable without losing previous information.
  1. What variable type can be used in order to solve the problem of storing multiple values?
  • Arrays
  1. What are properties in Javascript?
  • A value can have a property, like a string variable that has the property of its length (amount of characters). It is a characteristic of an object, often describing attributes associated with a data structure. There are two kinds of properties: Instance properties hold data that are specific to a given object instance. Static properties hold data that are shared among all object instances.
  1. Which values do not have properties?
  • Null and undefined
  1. How can we access properties in a value (two ways)?
  • value.property
  • value[“property”]
  1. What are methods?
  • Properties that contain functions are generally called methods of the value they belong to. For example .push method adds a value to an array.
  1. What are objects?
  • An object is a collection of properties.
  1. What problem do objects solve that cannot be solved with other value types we’ve learned so far (such as integer, string, array, boolean etc)?
  • An object can have multiple entries, expressing together a single condition.
  1. How do you define an object?
  • By using braces as an expression, where they are positioned not at the start of a statement.
let day1 = {

squirrel: false,

events: ["work", "touched tree", "pizza", "running"]

};
  • So here squirrel is a property of the variable day1, and events is an object in variable day1 that has several properties.
  1. What can you say about the mutability of Javascript objects?
  • Unlike strings, numbers or Booleans, objects are not immutable. You can change their properties, causing a single object value to have different content at different times.

Think about the following questions:

  1. Why can’t you add new properties to a string variable?
  • They already have fixed, built-in properties. You cannot change them because they are not a collection of properties like an object.*** ***If you try to assigne new properties to them, they will not be stored.
  1. What are rest parameters?
  • The rest parameter allows you to represent an indefinite number of arguments as an array. You can build a function containing first a set of arguments as variables and then the remaining arguments may be collected in an array in the same function. Maybe you could see performing the rest parameter as an unpivot?
function showName(firstName, lastName, ...titles) {
   alert( firstName + ' ' + lastName ); // Julius Caesar
   // the rest go into titles array
   // i.e. titles = ["Consul", "Imperator"]
   alert( titles[0] ); // Consul
   alert( titles[1] ); // Imperator
   alert( titles.length ); // 2
   }
  1. (Feel free to skip the sub-chapter of Math object and Destructing)
  2. What is serialisation and what is a use case of serialisation of data?
  • It means converting into a flat description, whereby you store data in a way that it references not to your local disk space memory coordinates so that data can be transferred to someone. Others cannot ready your data/code/analysis if it the code references to memory locations on your local drive.
  1. What is JSON?
  • JSON stands for JavaScript Object Notation. It is a serialization format that is widely used as a data storage and communication format. This language is similar to JavaScript but with a few less restrictions (like only double quotes allowed, no comments allowed, nothing involving computation allowed)
  1. What are the differences between JSON and the way programmers write objects in plain Javascript?
  • All property names have to be surrounded by double quotes, and only simple data expressions are allowed—no function calls, bindings, or anything that involves actual computation. Comments are not allowed in JSON.
1 Like

1. Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as strings or integers?
Jacques has the problem that he turns into a squirrel from time to time in the evening. In order to find out which events provoke or prevent turning him into a squirrel he writes a log every day containing events of that day, which might have an influence on this phenomenon. So in his log he has a list of events and the information, if he turned into a squirrel or not on that day.

2. What variable type can be used in order to solve the problem of storing multiple values?
The event list can be hold in an array of strings so that you can access the events of a certain day by interating through each element of the array. The second information, which is turning him into a squirrel or not turning him into a squirrel can be saved in a boolean. However since the array of events and the boolean belong together they should be glued together using an object. This ensures that you always have the necessary information available in the same object, when dealing with the data. The log is an array of the object described above.

3. What are properties in Javascript?
Almost all values in JavaScript have properties. For example you can access the length of a string by using its length property:

var myString = "ABC";
console.log(myString.length);

This will print ‘3’ to the console, since myString is consisting of 3 characters.

4. Which values do not have properties?
The values null and undefined do not have a property. You cannot stick a property to “nothing” or something you don’t know at all.

5. How can we access properties in a value (two ways)?
We can access the length property of our string myString in two ways:

myString.length or myString["length"]

Since the first notation is more succinct (using just a dot instead of a pair of double quotation marks inside a square bracket) it is used in most cases.

6. What are methods?
Properties that contain functions are generally called methods of the value they belong to:

var myString = "ABC";
console.log(myString.toLowerCase());
// → abc

By using the method toLowerCase on myString, all uppercase letters had been turned into lowercase letters.

7. What are objects?
Values of the type object are arbitrary collections of properties. Objects can be created by using curly brackets as an expression. The properties are listed inside the curly brackets and are separated by commas from each other. Each property has a name followed by a colon and a value. Properties whose names aren’t valid binding names or valid numbers have to be quoted. Numeric values and the values of booleans (true or false) are not quoted.

8. What problem do objects solve that cannot be solved with other value types we’ve learned so far (such as integer, string, array, boolean etc)?
Objects makes it easier to keep the data belonging to each other together. One log entry in our example is an object which consists of an array of strings with the events on that day and a boolean called squirrel. Of course you can also keep track of the log by creating an array of strings and an array of boolean. But in this case you will have to use some more code in order to get this problem implemented. For example if you put all events of a day in a single string separated by commas, you will have to convert this string into an array before you can access each event of a day. Otherwise you can use a two-dimensional array of strings to save each event of each day separately. However things will get ugly and complicated, because you have to reinvent the wheel. But with objects you can use built-in methods like in to check whether a particular event happened on day or not.
Inside our object the structure of it already reflects the structure of our problem. The daily events and the boolean squirrel belong to each other and are easily accessible since this data structure is contained in one object.

9. How do you define an object?
An object is a collection of different types and their values. The manipulation of objects can only be performed by using the defined methods. This protects your object from getting messed up from the outside by other objects. The complexity of the problem is already shown in the structure of the different types contained in the object. Thinking in terms of objects is more human-like and helps to implement complexity, while keeping the implementation of small methods as simple as possible.

10. What can you say about the mutability of Javascript objects?
Values of type string, number, and Boolean are not objects. Such values are immutable and cannot be changed.Objects work differently. You can change their properties, causing a single object value to have different content at different times.

SECOND PART:

1. Why can’t you add new properties to a string variable?
The type string is immutable, so it is impossible to change values of this type. You can try to put a new property to a string variable, but this will be ignored by JavaScript. The new property doesn’t stick, because strings are not objects.

2. What are rest parameters?
It can be useful for a function to accept any number of arguments. For example you can calculate the sum of an arbitrary number of values. When such a function is called, the rest parameter is bound to an array containing all further arguments. If there are other parameters before it, their values aren’t part of that array.

I will show the usage of rest parameters in a simple example. Let’s assume we want to calculate the average price in USD of a list of prices. We have two functions calcSum and calcAverage. The function calcSum takes an array of numbers and calculates the total sum of all values. Please note that I used the a simpler way to write such loops which is use in modern JavaScript, instead of using an index variable.

for (let summand of allSummands) {}

When a for loop looks like this, with the word of after a variable definition, it will loop over the elements of the value given after of. In our example summand will get each value of the array allSummands at one iteration and totalSum will sum up all the values.
The function calcAverage takes the currency as a parameter and the array allSummands as rest parameter. calcAverage calls the function calcSum to calculate the total sum of all prices and divides it by the number of values, that is the length of the array allSummands. The toFixed(2) statement rounds the result to 2 digits. At last a blank and the currency (“USD”) are added to the return value.

    <script>
    let allPrices = [3.99, 2.49, 6.99, 5.49, 0.99];
    function calcSum(...allSummands){
      var totalSum = 0;
      for (let summand of allSummands) {
        totalSum += summand;
      }
      return totalSum;
    }

    function calcAverage(currency, ...allSummands) {
      return ((calcSum(...allSummands)/numbers.length).toFixed(2) + " " + currency)
    }

    console.log(calcAverage("USD", ...allPrices));
    </script>

// → 3.99 USD

3. (Feel free to skip the sub-chapter of Math object and Destructing)
I wanted to present a kind of cheat sheet that will show you how things are related:


In the image above the meaning of this notation is shown.


Due to the different colors used in this picture you can figure out easily how to insert the values of our example into the formula.

In the image above you can see clearly how the formula has been translated into JavaScript code.

function tableFor(event, journal) {
  let table = [0, 0, 0, 0];
  for (let i = 0; i < journal.length; i++) {
    let entry = journal[i], index = 0;
    if (entry.events.includes(event)) index += 1; //event, for example pizza
    if (entry.squirrel) index += 2; // squirrel: Jacques turned into a squirrel!
    table[index] += 1;
  }
  return table;
}
console.log(tableFor("pizza", JOURNAL));
// → [76, 9, 4, 1]

In the following I want to explain how the function tableFor works:
The statement (entry.events.includes(event))will be true, if the event, for example pizza occured on that day. In this case the variable index will be incremented by 1.
The statement (entry.squirrel) will be true, if Jacques turned into a sqirrel on that day. In this case the variable index will be incremented by 2.


The variable index starts with the value 0. According to the conditions in the two if statements the index is incremented or not. It is like counting a score: You start with 0 points and for the pizza you get 1 point and for the squirrel you get 2 points. In the end you will have 0, 1, 2 or 3 points!
This way the variable index takes care of sorting the 4 different cases into the right box. The 4 boxes are represented by the array table.

4. What is serialisation and what is a use case of serialisation of data?
If you want to save data in a file for later or send it to another computer over the network, you have to somehow convert these data into a format that can be send or stored. Serialisation means that all data have to be transferred to a flat description or a sequence. The notations of this format have to be standardised.

5. What is JSON?
JSON stands for JavaScript Object Notation and is a format which is widely used as a data storage and communication format on the Web. Even many other programming languages have JSON interfaces. The JSON format is also widely used for communication between clients and servers via an Application Programming Interface (API).

6. What are the differences between JSON and the way programmers write objects in plain Javascript?
Compared to JavaScript’s way of writing arrays and objects JSON has a few restrictions:

  • All property names have to be surrounded by double quotes.
  • Only simple data expressions are allowed.
  • Function calls, bindings, or anything that involves actual computation is not allowed.
  • Comments are not allowed in JSON.
4 Likes

Thank you for this great answer. This explains it better than the book!

Ivo

3 Likes

I appreciate that you like my post. Since I am not a native speaker it also took me some time to understand this chapter. I read this chapter twice to get some more understanding about the topic. And then I had the idea to do this approach with the images.

In Germany we have this saying: A picture is worth a thousand words.

Thank you for your reply

Ouzo69

1 Like
  1. Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as strings or integers?
    Because the problem requires a data structure, which comprises of combinations of variable types.

  2. What variable type can be used in order to solve the problem of storing multiple values?
    Array

  3. What are properties in Javascript?
    Properties are the aspects of a value/binding that define specific characteristics of those values/bindings, such as ‘length,’ or ‘color’ or ‘the third item of the array’

  4. Which values do not have properties?
    Null and undefined

  5. How can we access properties in a value (two ways)?
    By using a dot or brackets

  6. What are methods?
    Properties that contain functions, such as function.toUpperCase, whereby function is a string or an array of strings.

  7. What are objects?
    Arbitrary (premeditated, albeit) collections of properties

  8. What problem do objects solve that cannot be solved with other value types we’ve learned so far (such as integer, string, array, boolean etc)?
    Objects combine other value types into groups than can then be used in a data structure.

  9. How do you define an object?
    An object is a defined set of properties that helps to group properties needed to build a data structure.

  10. What can you say about the mutability of Javascript objects?
    Objects can have different identities, and they are different even if they share the same properties. However, the properties within objects can be changed freely. That’s mutability.

SECOND PART:

  1. Why can’t you add new properties to a string variable?
    Because a string variable is not an object.

  2. What are rest parameters?
    They are parameters that hold all arguments.

  3. (Feel free to skip the sub-chapter of Math object and Destructing)

  4. What is serialisation and what is a use case of serialisation of data?
    Serialization is a process to convert data to a flat description so that it can be easily stored or sent.

  5. What is JSON?
    JavaScript Object Notation, a popular serialization format.

  6. What are the differences between JSON and the way programmers write objects in plain Javascript?

1 Like

Hey guys, I was having a lot of trouble understanding loops and arrays/objects, so I did the following line by line description to help myself learn these concepts. Hope it helps!

Weresquirrel Example - Detailed Run-Through of the function tableFor(event, journal)

function tableFor(event, journal) { // This part defines the function with parameters of event, which is the actual string(s) of the events in the arrays, and the parameter of journal, which is the large journal var

let table = [0, 0, 0, 0]; // Next is the table var within the function, an array of 4 numbers, in the sense of [no event/no squirrel, yes event/no squirrel, no event/yes squirrel, yes event/yes squirrel]

for (let i = 0; i < journal.length; i++) { // for loop counter i to journal.length, which is the number of entries in the journal itself

**let entry = journal[i], index = 0;** // let var entry journal[0]th entry, index beginning at 0 within each journal entry,

**if (entry.events.includes(event)) index += 1;** // if within the events Array in each journal entry includes the name of an event, such as “work”, add 1 to index.  Note that index=0 is no event, so if there is an event, we add 1 in count to it, since the 2nd number in the table denotes yes event/no squirrel.

**if (entry.squirrel) index += 2;** // if the entry also has squirrel=true, then it adds 2 to the index

**table[index] += 1;** // the loop then adds 1 to the appropriate VALUE of that index property within the table

}

return table; // So, in the example in the book, this table returns [76, 9, 4, 1]

}

Let’s use the following journal data for the 4 possible event pairs, looking at the event called “carrot":

var JOURNAL = [

{“events”:[“exercise”,“weekend”],"squirrel”:false}, // no event, no squirrel

{“events”:[“carrot”,“weekend”,“touched tree”],“squirrel”:false}, // yes event, no squirrel

{“events”:[“nachos”,“brushed teeth”,“cycling”,“weekend”],"squirrel”:true}, // no event, yes squirrel

{"events”:[“carrot”,"brussel sprouts”],"squirrel”:true}, // yes event, yes squirrel

];

So, when we run function tableFor(“carrot”, JOURNAL), let’s go through each entry in JOURNAL through the code and see what happens.

Entry 1: {“events”:[“exercise”,“weekend”],"squirrel”:false} // no event, no squirrel

table = [0,0,0,0] - table[0]=0, table[1]=0, table[2]=0, table[3]=0, where index as defined below refers to the ORDER of the item in brackets, NOT the value of the item itself. This is crucial!

i = 0; i < JOURNAL.length, which is 4 entries, so i will go up to 3, a total of 4 iterations

entry = JOURNAL[0], index = 0; so we start at ORDER=0 of the 1st entry

If (entry.events.includes(“carrot”)) then index to add 1, since no event, so index remains = 0

If (entry.squirrel) then index to add 2 no squirrel, so index remains = 0

table[index] +=1, which is table[0] since index is still 0, so, we add 1 to the VALUE of the FIRST item in the table, thus:

return table, which is = [1,0,0,0]

Entry 2: {“events”:[“carrot”,“weekend”,“touched tree”],“squirrel”:false}, // yes event, no squirrel

table = [1,0,0,0] - table continued from after processing the entry above

i = 1; i < 4,

entry = JOURNAL[1], index = 0; so we start at ORDER=0 of the 2nd entry

If (entry.events.includes(“carrot”)) then index to add 1, since yes event, so index = 1

If (entry.squirrel) then index to add 2 no squirrel, so index remains = 1

table[index] +=1, which is table[1] since index is 1, so, we add 1 to the VALUE of the SECOND item in the table, thus:

return table, which is = [1,1,0,0]

Entry 3: {“events”:[“nachos”,“brushed teeth”,“cycling”,“weekend”],"squirrel”:true}, // no event, yes squirrel

table = [1,1,0,0] - table continued from after processing the entry above

i = 2; i < 4,

entry = JOURNAL[2], index = 0; so we start at ORDER=0 of the 3rd entry

If (entry.events.includes(“carrot”)) then index to add 1, since no event, so index = 0

If (entry.squirrel) then index to add 2 yes squirrel, so index +2 = 2

table[index] +=1, which is table[2] since index is 2, so, we add 1 to the VALUE of the THIRD item in the table, thus:

return table, which is = [1,1,1,0]

Entry 4: {"events”:[“carrot”,"brussel sprouts”],"squirrel”:true}, // yes event, yes squirrel

table = [1,1,1,0] - table continued from after processing the entry above

i = 3; i < 4, - final run through this loop

entry = JOURNAL[3], index = 0; so we start at ORDER=0 of the 4th and final entry

If (entry.events.includes(“carrot”)) then index to add 1, since yes event, so index = 1

If (entry.squirrel) then index to add 2 yes squirrel, so index +2 = 3

table[index] +=1, which is table[3] since index is 3, so, we add 1 to the VALUE of the FOURTH item in the table, thus:

return table, which is = [1,1,1,1]

So, from this function, we can see that when we run an string type variable as the event using our JOURNAL object variable, we will compile a table array of 4 values, each representing one of 4 outcomes of our event pairing with our squirrel outcomes.

edit @ivga80:

1 Like
  1. Read the sub-chapter called The weresquirrel. What problems does this chapter introduce that cannot be solved with variable types such as strings or integers?

Weresquitrrel introduces the problem of having so many different values that all relate to many different objects.
2. What variable type can be used in order to solve the problem of storing multiple values?

We can use ‘arrays’ in order to solve the problem of storing multiple values.
3. What are properties in Javascript?

Properties in JS define characteristics within a values data structure.
4. Which values do not have properties?

Neither ‘Null’ or ‘undefined’ have any properties.
5. How can we access properties in a value (two ways)?

We can access properties within a value 1 of 2 ways;

value[x];
||
value.x;
  1. What are methods?

Methods are properties that hold a type of function. Much like the .click() method, whereas, the moment a button is clicked something happens.
7. What are objects?

Objects are a collection of different properties.
8. What problem do objects solve that cannot be solved with other value types we’ve learned so far (such as integer, string, array, boolean etc)?

Objects allow programers to store multiple properties all within one defined data structure.
9. How do you define an object?

Objects hold all of their data within braces. Properties are separated by commas and their values come after a colon.
10. What can you say about the mutability of Javascript objects?

Seeing as we can hold multiple properties about our webpage all within one object, then we can easily apply methods to redefine the whole object or just certain parts.

SECOND PART:

  1. Why can’t you add new properties to a string variable?

A string variable is completely defined by the bytes it uses. String variables are immutable.
2. What are rest parameters?

Rest parameters tell the computer where the parameters of an array is when we call in all possible elements[…].

  1. What is serialisation and what is a use case of serialisation of data?

Serialization is the process our computer takes to create a path for our information to be logged in specific bits of information. It may be used to save or send data through a computer or network.
5. What is JSON?

JSON stands for JavaScript Object Notation and is a very static programming language that is used for data storage and communication over the web.
6. What are the differences between JSON and the way programmers write objects in plain Javascript?

There are subtle differences between JSON and JS. Including how we use quotations, brackets, and letter case.

2 Likes

The mentioned variables cannot store multiple values, which needed in this case.
2.
Arrays
3.
Properties are association between name and value.
Properties refer to the collection of values which are associated with the JavaScript object
4.
Null and undefined
5.
Dot notation . or the square bracket notation [ ].
6.
Actions that can be performed on objects.
A JavaScript method is a property containing a function definition.
7.
Unordered collection of related data, of primitive or reference types, in the form of “key: value” pairs.
Variables which can contain many values
8.
Problems that involve multiple values. The mentioned types are immutable types, means that in those types impossible to change values.
9.
“Object.defineProperty()”
Adds the named property described by a given descriptor to an object.
10.
Changing objects properties, causing a single object value to have different content at different times.

Second part:
1.
String type is immutable, cannot be changed, modified.
2.
Useful for a function to accept any number of arguments.
Allowing us to more easily handle various input as parameters in a function.
3.

A mechanism for storing an object’s states into a persistent storage like disk files, databases, or sending object’s states over the network.
Use case:
It is a process used to convert the state of an object into a byte stream, which can be persisted into disk/file or sent over the network to any other running Java virtual machine.
5.
It is a serialisation format. (JavaScript Object Notation)
6.
All property names have to be surrounded by double quotes, and only simple data expressions are allowed - no function calls, bindings, or anything that involves actual computation. Comments are not allowed in JSON.

1 Like