Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment covers module 3 and module 4 topics. To write the programs below

ID: 3846567 • Letter: T

Question

This assignment covers module 3 and module 4 topics. To write the programs below you will need to demonstrate an understanding of :

Functions

Arrays

Random Numbers

variables

simple data types

expressions

loops

Please refer back to the module lectures if you find you have any gaps in your understanding for these topics.

1. (40 pts) Write a JS program called "assignment_2.py". In this script create the following functions:

a. A function called larger that returns the larger of its two arguments. For example, the call larger(-2, 10) should return 4.

b. A function that returns the average of its two arguments. For example, the call average(-2,10) should return 4.

c. A function called averageLst that returns the average of all of the items in an array. For example, averageLst([4, 5, 7, 2]) should return 4.5.

d. A function called randNum, that accepts two numbers and returns a random number between the two values.

e. A function called median, that accepts three values and returns the median. The median of three values is the value that is greater than or equal to one of the other values and less than or equal to the remaining value.

f. A function called numberOfZeroes that returns the number of zeros in a given array. For example, the call numberOfZeroes([4, 0, false, 5, 0]) should return 2.

g. A function called cntOccurrences, that returns the number of occurrences of a given character in a string. For example, given the string "Rat-a-tat-tat" and the character t, your function should return 5.

h. A function called isPrime, that takes an array of numbers and returns an array containing the numbers that are prime. For example. The call isPrime([1, 5, 17, 25, 30]) should return [1, 5, 17].

2. (25 pts) Now we need to use our functions. Write calls to each of the functions you wrote above and use the following data for arguments:

a. 15, 100

b, 80, 50

c. [75, 60, 100, 90]

d. 10, 20

e. [-10, 25, 0]

f. [10, 0, 0, 0, false, true, 0, 0]

g. "Adrian Tillman was here", "a"

h. [1, 2, 3,5,7, 10, 12, 13, 15, 20]

3. (10 pts) Display the results. For each call, display the output such as:

"The result of calling numberOfZeroes with the values 10, 0, 0 is 2"

Explanation / Answer

1)

a.

function larger(a,b) {
return Math.max(a,b);
}

b.

function avg(a,b) {
return (a+b)/2;
}

c.

function averageLst(arr) {
sum=0.0
for (var i = 0; i < arr.length; i++) {
sum=sum+arr[i];
}
return sum/arr.length;
}

d.

f.

function numberOfZeros(arr) {
count=0
for (var i = 0; i < arr.length; i++) {
if(arr[i]==0)count=count+1;
}
return count;
}

e.

function calcMedian(a,b,c){

median=c;

if(a>=b&&a<=c)

median = a;

if(b>=c&&b<=a)

median = b;

return median;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote