CS 141 Final Review Part 2

Ace your homework & exams now with Quizwiz!

What is the value of price at the end of this code? var price = 60; var calcTotal = function( couponAmount ){ price = 30; return price - couponAmount ; } alert( calcTotal(10) ); //What is the price now?

price is 30 because that is the last value assigned to the variable. there is not "var" before the variable inside the function so this will be a global variable even though it is inside the function

After the following code executes, the value of x is: var x=1; var y=1; if ( x===2) { y=2; }

x will = 1 because it is assigned that value as a global variable and not reassigned within the if statement

After the following code executes, the value of x is: var x=1; var y=1; if ( x=2) { y=2; }

x will = 2 because the value is being reassigned in the if statement

After the following code executes, the value of y is: var x=1; var y=1; if ( x===2) { y=2; }

y will = 1 because the if statement is false, so the value of y will not be reassigned

After the following code executes, the value of y is: var x=1; var y=1; if ( x=2) { y=2; }

y will = 2 because the if statement is always going to be true

After the following code executes, y is: var x=1; var y=1; if ( x==2) { y=2; } else { y=3; }

y will = 3 because the initial if statement is false, so the else statement will reassign the value of y to 3

Which operator should I put in the if statement in place of the ?? to make the alert "Good Afternoon" if it is 1200 or after and "Good Morning" if is is before 1200. var militaryTime = Number( prompt ("What time is it?") ); if ( militaryTime ?? 1200 ) { alert("Good morning"); } else { alert("Good afternoon"); }

<

The following function is meant to add num1 to num2 and return the result of the addition. Write the line or lines of code that will return the sum of num1 and num2 from the function. var sum = function(num1, num2) { //what line of code is needed here? };

(return num1 + num2;)

Explain the difference between a function call and a function definition (Examples are encouraged, but a written description is required.)

Defining a function is when you physically write out the function and what you want it to do. For example... var number= function() { alert("this is a number"); } However, this code will not actually be executed/run until the code is called. For example.... number(); Basically, defining the function means creating it while calling the function is actually running the code within the function to get the outcome.

What is the result of the following code: var cheer = function(state) { if (state == "WI") { alert("Go Packers"); } else if (state == "MN") { alert("Go Vikings"); } }

Nothing will alert because the function is only being defined, it is not being called/ run

Explain the difference between using the equal sign for assignment vs. comparison. (Examples are encourage, but written description is required.)

One equal sign (=) is used for assignment. this means that the variable is going to take on whatever value is on the other side of the equal sign. For example... var number= 5 Now, the variable number is storing the value of 5. Two equal signs (==) is used for a comparison. This is valuable in things such as if statements because you want to say "if this is true, then do this." In this example, you would need to use two equal signs because you are basically asking the computer "is this true? if so, do this." If you were to use one equal sign, you would be giving that variable the value in question, meaning that the if statement is always going to be true, which is not something we necessarily want. If Statement example... if (number == 5) { alert("Good Job."); }

What is the value of price at the end of this code? var price = 60; var calcTotal = function(price, couponAmount ){ var price = 30; return price - couponAmount ; } alert( calcTotal(50,10) ); //What is the price now?

Price will be 60 because it is a global variable defined outside of the function and that will overpower a local variable while outside of the function

What is the value of price at the end of this code? var calcTotal = function(price, couponAmount ){ var price = 30; return price - couponAmount ; } alert( calcTotal(50,10) ); //What is the price now?

Price will be undefined because it is a local variable and not defined outside of the function

What is the result of the following code: var price = 30; var couponAmount = 10; var calcTotal = function(price, couponAmount ){ return price - couponAmount ; } alert( calcTotal(50,10) );

There will be one alert message: 40 because the function holds parameters so when the function is called the parameters are taken in and used instead of the previously defined variable value

The following code will alert "Hello" var condition= false; if (condition) { alert("Hello"); } else { alert("Good-bye"); }

false - the code will alert "good-bye" because condition = false, therefore the if statement will be false and the else statement will run

The variable yPos is global given the following code: var xPos = 200; var drawFlea = function () { var yPos = 200; fill(97, 93, 93); ellipse(xPos,yPos,20,10); fill(255,255,255); ellipse(xPos,yPos-10,10,15); ellipse(xPos+5,yPos-10,10,15); }

false because the variable yPos is only defined within the function making it a local variable

The following code defines a function: var sayHello= function () { var textX = random(0, 300); var textY = random(0, 300); fill(255, 0, 0); textSize(30); text("Hello",textX, textY); }; Write the JavaScript code to call this function which causes the code within the braces to be executed. Note: you do not need to rewrite the function. Your answer should be a single line of code.

sayHello (); will call the function

JavaScript is case sensitive which means that the variable xPos is not the same as xpos.

true

The variable xPos is global given the following code: var xPos = 200; var drawFlea = function () { var yPos = 200; fill(97, 93, 93); ellipse(xPos,yPos,20,10); fill(255,255,255); ellipse(xPos,yPos-10,10,15); ellipse(xPos+5,yPos-10,10,15); }

true - because it is defined outside of the function

The following code will alert the message. if ( true ) { alert("The boolean true is always true"); }

true - because the if statement is always going to be true

What is the result of the following code: var cheer = function(state) { if (state == "WI") { alert("Go Packers"); } else if (state == "MN") { alert("Go Vikings"); } } cheer("WI"); cheer("MN");

two messages will alert the first being "go packers" the second being "go vikings"

Change the following function to take parameters to specify the position to write the greeting when the function is called rather than always writing it at (20,20). For example, after you rewrite the function, I can call: greeting(20,20); greeting(30,55); Here is the function to change. var greeting = function () { var xPos = 20; var yPos = 20; fill(255, 0, 0); textSize(30); text("Hello", xPos, yPos); };

var greeting = function ( xPos, yPos) { fill(255, 0, 0); textSize(30); text("Hello", xPos, yPos); };

Copy the following code and modify the function so that it writes the message "Congratulations on ordering a tank top." when the style is tank in addition to keeping the messages of the current cases for "tech" and "tee". var orderShirt= function(style) { if (style == "tech") { alert("Congratulations on ordering a moisture tech shirt."); } else { alert("Congratulations on ordering a t-shirt."); } } orderShirt("tech"); orderShirt("tank"); orderShirt("tee");

var orderShirt= function(style) { if (style == "tech") { alert("Congratulations on ordering a moisture tech shirt."); } else if (style == "tank") { alert("Congratulations on ordering a tank top."); } else { alert("Congratulations on ordering a t-shirt."); } } orderShirt("tech"); orderShirt("tank"); orderShirt("tee");

Which of the following is not a debugging technique recommended by the professor:

writing the full program before running any of the code so you can get the final answer to check the result


Related study sets

ANTH 344 Altruism & Kin Selection

View Set

Chapter 8: Sarbanes-Oxley, Internal Control, & Cash

View Set

Physics: Energy and momentum study guide 2

View Set