Question

Define a JavaScript function named activityScore with one parameter. Assume the function will be called with...

Define a JavaScript function named activityScore with one parameter. Assume the function will be called with an array of Numbers. Define the function so that it creates an Object. This object should have a key equal to "score", and its paired value should be set to the sum of the array’s entries. Return the JSON blob encoding the Object.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Activityscores.html:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <!-- title for web page -->

    <title>Activity Scores</title>

</head>

<body>

    <!-- script is used for javascript -->

    <script>

    //function activityScore with one parameter numbers

    //here numbers is an array

    //this function will sum all the array elements and

    //return the JSON object

    function activityScore(numbers){

        var total=0;//variable used to store total of all array elements

        //using for loop to add each variable from the array numbers

        for(var k=0;k<numbers.length;k++)

        {

            //adding element from the array numbers to the variable total

            total+=numbers[k];

        }

        //creating object with key score and value is total

        var object='{"score":'+total+'}';

        //returning object as JSON

        return JSON.parse(object);

    }

    //call function activityScore() and print the sum of array elements

    console.log("activityScore( [ 25, 30, 32 ]):"+activityScore([25,30,32]).score);

   

    </script>

</body>

</html>

=========================

Output :

Add a comment
Know the answer?
Add Answer to:
Define a JavaScript function named activityScore with one parameter. Assume the function will be called with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a JavaScript function named showScores which has one parameter which is a JSON blob (JSON...

    Define a JavaScript function named showScores which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an object which maps strings to Numbers. This object will have the keys: "total" and "count". Your function should display the parameter's value associated with "total" in a div element whose id is "overall" . It must also display the parameter's value associated with "count" in a div element whose id is "num".

  • Create a JavaScript function named display_data that has two parameters. The first parameter will be a...

    Create a JavaScript function named display_data that has two parameters. The first parameter will be a String containing a JSON blob like the function in part 1 returns. The second parameter will be a boolean. Start by using the JSON library to convert the first parameter into a usable JavaScript Object. Next get the HTML element whose id is "data". This HTML element is a div. If the second parameter is equal to true ​, write the value associated with...

  • javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the...

    javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the format of an array of objects where each object has keys "mass", "density", "temperature", and "velocity" and each key maps to a floating point number. This function should return the average "velocity" of all the objects in the array as a JSON string in the format {"velocity": }

  • In JavaScript define a function named 'lowInventory' which will be called with a dictionary mapping strings...

    In JavaScript define a function named 'lowInventory' which will be called with a dictionary mapping strings to numbers and which returns the number of key-value pairs whose value is lee than or equal to 10. Example: lowInventory({'milk':14,'butter':5,'bread':37,'jam':8}) must return 2 lowInventory{{'oil':16,'filter':29,'grease':42,'hydraulic fluid':18}) must return 0

  • Functionality to build (4 functions) read_csv_header Define a function named read_csv_header with one parameter. This parameter...

    Functionality to build (4 functions) read_csv_header Define a function named read_csv_header with one parameter. This parameter will be a csv reader object (e.g., the value returned when calling the function csv.reader). The first row read using the parameter is the file's header row. The header row contains the keys for the data stored in the CSV file. This function must return a list containing the strings from that header row. The function parameter will be a csv reader object and...

  • use javascript please Complete the function below using recursion. Assume the parameter is a positive integer....

    use javascript please Complete the function below using recursion. Assume the parameter is a positive integer. The function will return an array that contains all of the numbers from 1 up to the parameter in consecutive order. For example, if the parameter is the value 5 then the return value should be the array [1, 2, 3,4 function makeSequence(value) t\ please use recursion thank von

  • Create a function in JavaScript called "sum" that takes in an array as a parameter and...

    Create a function in JavaScript called "sum" that takes in an array as a parameter and returns the sum of all of the EVEN numbers in a given array. Please note: the array passed in can have any data type, i.e. Strings, Numbers, Booleans, undefined, etc. HINT: the expression typeof will help discern var types. For example if I have: var x = 4, and I do typeof(x) - the output would be "Number" Example run: sum([1, 4, 8, 10,...

  • create a javascript function named bbqFood that takes in an object and returns an array. The...

    create a javascript function named bbqFood that takes in an object and returns an array. The input object will have strings naming party foods as the key and the numbers of people that each key will feed as its values. return an array containing all of the keys whose values are greater than 10, menaing it will feed more than 10 people. example bbqfood(['taco':15, 'hamburger':6,'pasta':42,'fruit':23}) must return ['taco','pasta','fruit']

  • Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating...

    Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating point numbers. The function will make an HTTPS GET request to the url "https://fury.cse.buffalo.edu/ps-api/a" with a query string containing the same key-value pairs from the input key-value store. The response from the server will be a JSON string representing an object in the format "{"answer": <Number>}" where <Number> is a floating point Number. Return the value at the key "answer" as a float Recall...

  • python:Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating...

    python:Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating point numbers. The function will make an HTTPS GET request to the url "https://fury.cse.buffalo.edu/ps-api/a" with a query string containing the same key-value pairs from the input key-value store. The response from the server will be a JSON string representing an object in the format "{"answer": }" where is a floating point Number. Return the value at the key "answer" as a float Recall that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT