Question

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".

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

JAVASCRIPT FUNCTION:

function showScores(json){
            // Parse JSON blob
            var parsedJSON = JSON.parse(json);
            document.getElementById('overall').innerText = parsedJSON.total;
            document.getElementById('num').innerText = parsedJSON.count;
        }

COMPLETE HTML CODE WITH EMBEDDED JAVASCRIPT TO DEMONSTRATE ABOVE FUNCTION:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <button id="btn">Click here</button>
    <div id="overall"></div>
    <div id="num"></div>
    <script>
        function showScores(json){
            // Parse JSON blob
            var parsedJSON = JSON.parse(json);
            document.getElementById('overall').innerText = parsedJSON.total;
            document.getElementById('num').innerText = parsedJSON.count;
        }
        document.getElementById('btn').addEventListener('click', function(){
            var json = JSON.stringify({ "total": 12, "count": 5});
            showScores(json);            
        });
    </script>
</body>
</html>

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Define a JavaScript function named showScores which has one parameter which is a JSON blob (JSON...
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
  • 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...

  • 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.

  • 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": }

  • 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...

  • In JavaScript the inner function can access the variables from the outer scope even after the...

    In JavaScript the inner function can access the variables from the outer scope even after the outer function has returned. This is because the variables are still bound in the inner function and not dependent on the outer function. This is called closure. True False 3 points    QUESTION 2 Which of the following statements is true about JSON? a. JSON stands for JavaScript Object Notation. b. JSON is a syntax for storing and exchanging data. c. JSON is text,...

  • Write a function named "find_key" that takes a key-value store as a parameter with strings as...

    Write a function named "find_key" that takes a key-value store as a parameter with strings as keys and integers as values. The function returns a boolean representing true if the string "focus" is in the input as a key, false otherwise(javascript)

  • 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

  • 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']

  • In JavaScript, Define a function which takes a number as a parameter and if the number...

    In JavaScript, Define a function which takes a number as a parameter and if the number is less than 0 or higher than 100, display the message, “the number is NOT a valid number”, otherwise display “the number is a valid number”.

  • 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...

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