Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function json_average(data) {
    data = JSON.parse(data);
    var total = 0;
    for (var i = 0; i < data.length; i++)
        total += data[i]["velocity"];
    return JSON.stringify({"velocity": total/data.length});
}

var data = [
    {"velocity": 10},
    {"velocity": 20},
    {"velocity": 15},
    {"velocity": 17},
];
console.log(json_average(JSON.stringify(data)));
Add a comment
Know the answer?
Add Answer to:
javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the...
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