Question

In: Computer Science

Activity 10: Review Comparison Operators Comparison operators allow you to test whether values are equivalent or...

Activity 10: Review
Comparison Operators
Comparison operators allow you to test whether values are equivalent or whether values are identical.


Conditional Code
Sometimes you only want to run a block of code under certain conditions. Flow control — via if and else blocks — lets you run code only under certain conditions.


Switch Statement
Rather than using a series of if/else if/else blocks, sometimes it can be useful to use a switch statement instead. [Definition:
Switch statements look at the value of a variable or expression, and run different blocks of code depending on the value.]





Loops
Loops let you run a block of code a certain number of times. Note that in Loops even though we use the keyword var before the variable name i, this does not “scope” the variable i to the loop block

For Loop
A for loop is made up of four statements and has the following structure:


The initialisation statement is executed only once, before the loop starts. It gives you an opportunity to prepare or declare any variables.
The conditional statement is executed before each iteration, and its return value decides whether or not the loop is to continue. If the conditional statement evaluates to a falsey value then the loop stops.
The iteration statement is executed at the end of each iteration and gives you an opportunity to change the state of important variables. Typically, this will involve incrementing or decrementing a counter and thus bringing the loop ever closer to its end.
The loopBody statement is what runs on every iteration. It can contain anything you want. You’ll typically have multiple statements that need to be executed and so will wrap them in a block ( {...}).

While Loop
A while loop is similar to an if statement, except that its body will keep executing until the condition evaluates to false.

An example for a while loop:




The do-while loop
This is almost exactly the same as the while loop, except for the fact that the loop’s body is executed at least once before the condition is tested.



An example for a do-while loop:



Functions
Functions contain blocks of code that need to be executed repeatedly. Functions can take zero or more arguments, and can optionally return a value.
Functions can be created in a variety of ways:

Function Declaration



Named Function Expression

A Simple Function
A Function Returns a Value


A Function that Returns another Function

A Self-executing Anonymous Function
A common pattern in JavaScript is the self-executing anonymous function. This pattern creates a function expression and then immediately executes the function. This pattern is extremely useful for cases where you want to avoid polluting the global namespace with your code — no variables declared inside of the function are visible outside of it.




Function as Arguments
In JavaScript, functions are “first-class citizens” — they can be assigned to variables or passed to other
functions as arguments. Passing functions as arguments is an extremely common idiom in jQuery.
• Passing an anonymous function as an argument


• Passing a named function as an argument


Task 1 – Write web program to calculate a class average mark. It should allow users to enter one mark at time for 3 times. See Lesson 10 slides 33-35 for references.



Task 2 – Write web program to calculate a class average mark. It should allow users to enter one mark at time for as many times as they wish to do so. It also allows user to stop by entering -1.

Solutions

Expert Solution

TASK 1 -

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function findAvg() {


   var Avgmarks = 0;//THE TOTAL VARIABLE IS INITIALIZED TO 0
   var arr = [];//THE ARRAY TO SAVE THE NUMBERS
   for(var i = 0; i < 3; i++) {
    var num = prompt("Enter marks");//TAKES INPUT
    arr.push(+num);//CONVERTS THE STRING INPUT TO INT AND STORES TO ARRAY
  
}
for (var i=0; i < arr.length; i++) {
        Avgmarks += arr[i];//CALCULATES THE TOTAL
        var avg = (Avgmarks/arr.length);//FINDS THE AVERAGE
}

document.write(avg);//PRINTS THE AVERAGE
}
findAvg();
</script>
</head>
<body>
</body>
</html>

-------------------------------------------------------------------------------------------

TASK 2-

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function findAvg() {

// var num = prompt("Enter marks");
   var Avgmarks = 0;//THE TOTAL VARIABLE IS INITIALIZED TO 0
   var arr = [];//THE ARRAY TO SAVE THE NUMBERS
   while(1) {//INFINITE LOOP TO ENTER AS MANY ENTER NUMBERS AS MANY TIMES YOU WANT
    var num = prompt("Enter marks");//TAKES THE INPUT
    if(+num == -1) break;//IF -1 ENTERED BREAK THE LOOP
    arr.push(+num);//ELSE PUSH THE NUMBER TO ARRAY
  
}
for (var i=0; i < arr.length; i++) {
        Avgmarks += arr[i];//CALCULATES THE TOTAL
        var avg = (Avgmarks/arr.length);//FINDS THE AVERAGE
}

document.write(avg);//PRINTS THE AVERAGE
}
findAvg();
</script>
</head>
<body>
</body>
</html>


Related Solutions

Directions: Review the marshmallow test videos in learning activity #1. As you revisit these videos/read the...
Directions: Review the marshmallow test videos in learning activity #1. As you revisit these videos/read the article, reflect on the concepts about motivation that are covered in this chapter. https://www.youtube.com/watch?v=4y6R5boDqh4 https://www.youtube.com/watch?v=KPZ5R9EA968 https://www.newyorker.com/magazine/2009/05/18/dont-2 In part 1 of this assignment, describe in 250 words how these concepts/theories explain the behavior exhibited by the children who participated in the marshmallow test. Example: Intrinsic vs. Extrinsic, Drive theory, Arousal Theory, etc. In part 2 of this assignment, analyze the children’s emotional reaction to the...
My Values: In this section you will write about your values, such as whether you follow...
My Values: In this section you will write about your values, such as whether you follow individualistic or collective values or a combination of both. Explain some of these individualistic or collective values. Also in this section write about an issue that you are passionate about such as gender equality, climate change, education or any other issue that interests you and say what you can do to contribute positively to this issue.
Bernoulli’s Principle This activity will allow you to see Bernoulli’s Principle in action. Some of the...
Bernoulli’s Principle This activity will allow you to see Bernoulli’s Principle in action. Some of the effects of fast moving air can be rather surprising. Equipment Cup Straw Index cards Blow Here Water Figure 1: The straws for Procedure 1 Procedure 1 Fill a small cup with water. Cut the plastic straw in half. Hold one piece vertically in the water, and the other in your hand. Position the straws so that they are perpendicular to each other, and arranged...
To test whether extracurricular activity is a good predictor of college success, a college administrator records...
To test whether extracurricular activity is a good predictor of college success, a college administrator records whether students participated in extracurricular activities during high school and their subsequent college freshman GPA. Extracurricular Activity College Freshman GPA Yes 3.57 Yes 3.34 Yes 3.90 Yes 3.67 No 3.00 No 3.88 No 3.45 No 2.80 No 3.90 No 2.82 A) Code the dichotomous variable and then compute a point-biserial correlation coefficient. (Round your answer to three decimal places.) _____
To test whether extracurricular activity is a good predictor of college success, a college administrator records...
To test whether extracurricular activity is a good predictor of college success, a college administrator records whether students participated in extracurricular activities during high school and their subsequent college freshman GPA. LEFT side of column: Activity RIGHT side: College GPA (a). Code the dichotomous variable and then compute a point-biserial correlation coefficient. (Round your answer to three decimal places.) Yes 3.54 Yes 3.28 Yes 3.96 Yes 3.71 No 3.00 No 3.82 No 3.43 No 2.79 No 3.89 No 2.75
To test whether extracurricular activity is a good predictor of college success, a college administrator records...
To test whether extracurricular activity is a good predictor of college success, a college administrator records whether students participated in extracurricular activities during high school and their subsequent college freshman GPA. Extracurricular Activity College Freshman GPA Yes 3.49 Yes 3.31 Yes 3.94 Yes 3.69 No 2.95 No 3.86 No 3.47 No 2.76 No 3.85 No 2.78 (a) Code the dichotomous variable and then compute a point-biserial correlation coefficient. (Round your answer to three decimal places.) (b) Using a two-tailed test...
To test whether extracurricular activity is a good predictor of college success, a college administrator records...
To test whether extracurricular activity is a good predictor of college success, a college administrator records whether students participated in extracurricular activities during high school and their subsequent college freshman GPA. Extracurricular Activity College Freshman GPA Yes 3.48 Yes 3.30 Yes 3.87 Yes 3.75 No 3.00 No 3.86 No 3.39 No 2.75 No 3.86 No 2.76 (a) Code the dichotomous variable and then compute a point-biserial correlation coefficient. (Round your answer to three decimal places.) 2. A psychologist noted that...
To test whether extracurricular activity is a good predictor of college success, a college administrator records...
To test whether extracurricular activity is a good predictor of college success, a college administrator records whether students participated in extracurricular activities during high school and their subsequent college freshman GPA. Extracurricular Activity College Freshman GPA Yes 3.57 Yes 3.32 Yes 3.86 Yes 3.72 No 2.93 No 3.88 No 3.46 No 2.71 No 3.86 No 2.84 (a) Code the dichotomous variable and then compute a point-biserial correlation coefficient. (Round your answer to three decimal places.)
Suppose you wish to test a hypothesis that two​ treatments, A and​ B, are equivalent against...
Suppose you wish to test a hypothesis that two​ treatments, A and​ B, are equivalent against the alternative that the responses for A tend to be larger than those for B. a. If the number of pairs equals 32​, give the rejection region for the​ large-sample Wilcoxon signed rank test for alpha equals α=0.10. b. Suppose that Upper T Subscript plusT+equals=404. State your test conclusions. c. Find the​ p-value for the test and interpret it.
Explain how arrays allow you to easily work with multiple values? Explain the relationship between the...
Explain how arrays allow you to easily work with multiple values? Explain the relationship between the length and valid indexes of an array? Why do we need to pass a size as well as the array to any function/procedure where we want it to use this array? Explain how for loops can be used to work effectively with elements in an array? How could you print out the names in the array starting with the last name and working back...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT