Question

In: Computer Science

// 5. Predict what the following code will print (write it down), then try it. var...

// 5. Predict what the following code will print (write it down), then try it.
var myVariable = "global";
var myOtherVariable = "global";

function myFunction() {
var myVariable = "local";
return myVariable;
}

function myOtherFunction() {
myOtherVariable = "local";
return myOtherVariable;

javascript

Solutions

Expert Solution

// global variables

var myVariable = "global";          // variable contains a string "global"

var myOtherVariable = "global";     // variable contains a string "global"


// function to return a string which is "local"

// variable inside the function is local scope local variable

function myFunction() {

    var myVariable = "local";       // variable contains a string "global"

    return myVariable;              // return the variable

}


// function to return a string which is "local"

// variable inside the function is local scope local variable

function myOtherFunction() {

    myOtherVariable = "local";      // variable contains a string "global"

    return myOtherVariable;         // return the variable

}

// display the global vars

console.log("My global var: " + myVariable);

console.log("My other global var: " + myOtherVariable);


// function calls to display the returned local vars

console.log("My local var: " + myFunction());

console.log("My other local var: " + myOtherFunction());

I HAVE EXPLAINED THE CODE IN USING COMMENTS.

IF YOU NEED FURTHER HELP PLEASE COMMENT.

THANK YOU


Related Solutions

Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
Write Java code for each of the following problem a. Two players, a and b, try...
Write Java code for each of the following problem a. Two players, a and b, try to guess the cost of an item. Whoever gets closest to the price without going over is the winner. Return the value of the best bid. If both players guessed too high, return -1. example: closestGuess(97, 91, 100) → 97 closestGuess(3, 51, 50) → 3 closestGuess(12, 11, 10) → -1 b. Given a non-empty string, return true if at least half of the characters...
Analyze the following Verilog code and write down its output as pictured in the code. module...
Analyze the following Verilog code and write down its output as pictured in the code. module blocking; reg [0:7] A, B; initial begin: init1 A = last decimal digit of your ID; #1 A = A + 1; // blocking procedural assignment B = A + 1; $display("Output 1: A= %b B= %b", A, B ); A = last decimal digit of your ID; #1 A <= A + 1; B <= A + 1; #1 $display ("Output 2: A=...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
What is the time complexity of the following code? (in big-O notaion) sum = 0 var...
What is the time complexity of the following code? (in big-O notaion) sum = 0 var = 0 product = 0 for i = 1 to (n+2){ sum = sum + 2i for j = i to [(n*n)+i]{ var = sum + var for k = sum + var{ product = var++ } } } for m + 1 to (j*i){ sum = sum + product }
Write down the VERILOG code for an XOR gate and the testbench code to test it
Write down the VERILOG code for an XOR gate and the testbench code to test it
the language is matlab 1) Write a code that will print a list consisting of “triangle,”...
the language is matlab 1) Write a code that will print a list consisting of “triangle,” “circle,” and “square.” It prompts the user to choose one, and then prompts the user for the appropriate quantities (e.g., the radius of the circle) and then prints its area. If the user enters an invalid choice, the script simply prints an error message. For calculating the area, create separate functions for each choice. Name them as calcTriangle, calcCircle, calcSquare respectively, which are only...
Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
Given a server.js file that uses express (i.e. var app = express();), write code to respond...
Given a server.js file that uses express (i.e. var app = express();), write code to respond to the routes below as specified in the description.  NOTE: all callback functions must be written in the new ES6 "Arrow Function" syntax: (1) Match the "GET" route "/users" and accept query string of "page=2&perPage=10" in the url, i.e. "/users? page=2&perPage=10". This route must show the following in browser: Query String Parameters: Page: 2 perPage: 10 Note: the query string parameters may have different values,...
write down the java code for garden sprinkler system.
write down the java code for garden sprinkler system.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT