Homework Solution: 1. (40 pts) Write a JS program called "assignment_2.js". In this script create the following fu…

    1. (40 pts) Write a JS program called "assignment_2.js". 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 10. 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. The function should take an array as it's argument. 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"

    Expert Answer

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

    1. (40 pts) Write a JS program named “assignment_2.js”. In this script beget the aftercited administrations:

    a. A administration named larger that receipts the larger of its brace topics. For issue, the flatter larger(-2, 10) should retaliate 10.

    b. A administration that receipts the mean of its brace topics. For issue, the flatter mean(-2,10) should retaliate 4.

    c. A administration named meanLst that receipts the mean of entire of the items in an adorn. For issue, meanLst([4, 5, 7, 2]) should retaliate 4.5. The administration should engage an adorn as it’s topic.

    d. A administration named randNum, that accepts brace mass and receipts a aimclose reckon between the brace rates.

    e. A administration named median, that accepts three rates and receipts the median. The median of three rates is the rate that is main than or similar to single of the other rates and close than or similar to the retaining rate.

    f. A administration named reckonOfZeroes that receipts the reckon of zeros in a loving adorn. For issue, the flatter reckonOfZeroes([4, 0, mock, 5, 0]) should retaliate 2.

    g. A administration named cntOccurrences, that receipts the reckon of occurrences of a loving sign in a string. For issue, loving the string “Rat-a-tat-tat” and the sign t, your administration should retaliate 5.

    h. A administration named isPrime, that engages an adorn of mass and receipts an adorn containing the mass that are consummate. For issue. The flatter isPrime([1, 5, 17, 25, 30]) should retaliate [1, 5, 17].

    2. (25 pts) Now we need to portraiture our administrations. Write flatters to each of the administrations you wrote over and portraiture the aftercited postulates for topics:

    a. 15, 100

    b, 80, 50

    c. [75, 60, 100, 90]

    d. 10, 20

    e. [-10, 25, 0]

    f. [10, 0, 0, 0, mock, gentleman, 0, 0]

    g. “Adrian Tillman was here”, “a”

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

    3. (10 pts) Evince the ends. For each flatter, evince the output such as:

    “The end of flattering reckonOfZeroes with the rates 10, 0, 0 is 2”

    Expert Tally

     

    1.administration larger(a,b)

    {

    retaliate Math.max(a,b)

    }

    2. administration avg(a,b)

    {

    retaliate (a+b)/2

    }

    3. administration meanLst(arr)

    {

    incorporate =0.0;

    for(var i=0;i<arr.length;i++)

    {

    incorporate = incorporate+arr[i];

    }

    retaliate incorporate/arr.length;

    }

    4. administration randNum(max,min)

    {

    retaliate Math.random() * (max – min) + min;

    }

    5.administration reckonOfZeros(arr)

    {

    enumerate =0;

    for(var i=0;i<arr.length;i++)

    {

    if(arr[i]==0)

    enumerate =count+1;

    }

    retaliate enumerate;

    }

    6. administration calcMedian(a,b,c)

    {

    median =c;

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

    median =a;

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

    median =b;

    retaliate median;

    }

    7. administration enumerate(string,char)

    {

    var a = novel RegExp(char,”g”);

    retaliate string.match(re).length;

    }

     

    8.

    var num = [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”];

    var txt = “”;

    var u;

    var tranquilize;

    for (i = 0; i < num.length; i++) {

    tranquilize = num[u]*1;

    text += “The reckon ” + tranquilize + ” is ” + isPrime(numb) + “<br>”;

    }

    document.getElementById(“demo”).innerHTML = text;

    administration isPrime(numb) {

    var strt = 2;

    while (strt <= Math.sqrt(numb)) {

    if (tranquilize % strt++ < 1)

    retaliate mock;

    }

    retaliate tranquilize > 1;

    }1.administration larger(a,b)

    {

    retaliate Math.max(a,b)

    }

    2. administration avg(a,b)

    {

    retaliate (a+b)/2

    }

    3. administration meanLst(arr)

    {

    incorporate =0.0;

    for(var i=0;i<arr.length;i++)

    {

    incorporate = incorporate+arr[i];

    }

    retaliate incorporate/arr.length;

    }

    4. administration randNum(max,min)

    {

    retaliate Math.random() * (max – min) + min;

    }

    5.administration reckonOfZeros(arr)

    {

    enumerate =0;

    for(var i=0;i<arr.length;i++)

    {

    if(arr[i]==0)

    enumerate =count+1;

    }

    retaliate enumerate;

    }

    6. administration calcMedian(a,b,c)

    {

    median =c;

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

    median =a;

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

    median =b;

    retaliate median;

    }

    7. administration enumerate(string,char)

    {

    var a = novel RegExp(char,”g”);

    retaliate string.match(re).length;

    }

     

    8.

    var num = [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”];

    var txt = “”;

    var u;

    var tranquilize;

    for (i = 0; i < num.length; i++) {

    tranquilize = num[u]*1;

    text += “The reckon ” + tranquilize + ” is ” + isPrime(numb) + “<br>”;

    }

    document.getElementById(“demo”).innerHTML = text;

    administration isPrime(numb) {

    var strt = 2;

    while (strt <= Math.sqrt(numb)) {

    if (tranquilize % strt++ < 1)

    retaliate mock;

    }

    retaliate tranquilize > 1;

    }