Leveraging Web Data Class 5 Homework

Answer the following questions about JavaScript. These questions may require you to do your own research to determine the best answer.


Developer Resources


Basic

Append your name to the text between the title tags.


Describe the purpose of JavaScript, how might it relate to web mapping and visualization?

Purpose of JavaScript is to help us interact with the webpage which otherwise would be static. It adds a behavior to respond to users actions. It can add animations and images and allows response to actions that a user takes within a webpage. It has the ability to manipulate data .It doesn’t have to wait for a response from external server as it is done on the user end. In web mapping and visualization it specifically allows us to interact with them. Rather than just a static map it will help us display or visualize some useful information related to the map. It can add 2D or 3D animations to the maps and visualization. D3 is an example of such a library that can be used in JavaScript to create useful data animations and transitions. It can also add data element to otherwise simple webpage. For example it can add let you see some data of certain area on a map simply by hovering over the map area.


Variables

What is a variable? Provide two examples of using a variable.

Variables are like containers that store data values For example we have a variable station and we want to give it some value, Var station = 5 Now anywhere the station is used it will use 5. Var x = 2 Var y = 5 Var z = x * y It will give var z a value of 10 We can use string instead of numbers Var name = taqi Here variable is name and its value is taqi.


Write out how to assign the variable x equal to 10.

Var x = 10


Operators

Describe the differences between =, ==, and ===.

= is used to assign a certain value to a variable. e.g X = 5 means x has a value 5 or is is equal to 5 == is used to check if something is equal to other. It converts the value to same type to check if they are same. It only compares values. === is also used to check if one thing is equal to other but additionally it compares both value and type.


What would be the result of "Hello " + 10? Why?

It would print 10 after Hello. Like Hello10 It is called unary + operator This plus operator tries to convert anything that comes after it into a function expression. It produces sum of both numeric and string operands.


Loops

What is the purpose of a for loop? Describe in detail the advantages of using the for loop.

Purpose of a for loop is to repeat a statement or step until a condition becomes false. It is an easy way to do something repeatedly. This significantly decreases the number of lines in the code. For loop is faster.


Describe the while loop in JavaScript.

It is very similar to for loop in that it runs as long as the condition is true. The condition is checked before the executing the statement. There are two parts of the while loop. The condition which deifnes when to run the loop e.g While (n < 5) This will run the loop As long as the value of n is less than 5 Second part is the statement which will be executed if the condition is true.


If/Else Statements

What are the three conditional statements? Describe how each are used.

Three conditional statements include If statement, which is used to execute the block of code if a specified condition is true. e.g if (x <= 12){ year = 1 } Else if is used to create a new condition to test if the first statement is false. Else if (x >12) { More than a year } Else statement, is used to execute a code if the condition is false for the if statement used before it. Else { Less than a year }


What is the difference between using two if statements together compared to using an if statement then an else if statement or else statement?

If statements are used when there are two entirely different condition and we are checking one condition at a time and only one response is given per statement. For example I am checking the month in a year. If (m = 6){ It is June } If (m = 9){ It is September } Here it will just check if the value of m is 6. If it is 6 it will display “It is june”. Otherwise it won’t do anything. It will go to the next if statement and only display the result if the condition for that is true. But when we are checking more than one conditions at time and the response is either one or the other we use else or else if For example If (m = 6){ It is june } Else if (m=9){ Its is September } Else { It is neither june nor September. } Here it will check the first condition for if. If it is true it will display the result. If it is false it will move to the second condition which is the else if statement and check if that is true. If that is true it will display its result otherwise it will move to the third condition that is else and if both if and else if are false it will surely print the statement after else condition.