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
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
ADVERTISEMENT