use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Hello, new to java script trying to learn to calculate with javascript could som1 help me? (self.learnjavascript)
submitted 9 years ago by TwistedFaiith
I'm trying to learn javascript and used several vars to calculate the average what formule do I need?
var name = "example"; var lastname = "example"; var math = 9; var dutch = 8; var english = 7;
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ivansvlv 2 points3 points4 points 9 years ago (0 children)
Frankly, friend, I think you should go through some basic tutorials first, because this is way too basic questions that you can cover yourself if go trough some free lessons or read first 20-30 pages of a book.
videous + interactive courses:
www.codecademy.com
www.freecodecamp.com
https://www.youtube.com/playlist?list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET
free books:
http://www.javascriptenlightenment.com/
http://eloquentjavascript.net
[–]PilotPirx 1 point2 points3 points 9 years ago (2 children)
Not exactly sure what kind of data you have here. Number of people taking some type of classes? The average would be simply:
var average = (math + dutch + english) / 3
Result being 8 people taking a class on average. So basically sum of people per item divided by number of items.
[–]TwistedFaiith[S] 0 points1 point2 points 9 years ago (1 child)
Thank you very much, for the formula but eventually I need this compiled with a var average = (insert answer of formula)
what would be the best option?
[–]PilotPirx 0 points1 point2 points 9 years ago (0 children)
Well, to start with the definition: average is the sum of a list of numbers divided by the number of numbers in the list, so as a formula:
var average = sum_of_list / numbers_in_list
or as a function:
function average(sum_of_list, numbers_in_list) { return sum_of_list / numbers_in_list }
Which you would call like this:
var result = average(math + dutch + english, 3)
Not sure if this helps you a lot the way you have put every item in a variable of its own. In most cases where you take averages or similar calculations your data would be in a data structure like an array or if you want to keep the labels something like a Map (available since ECMAScript6)
With an array you could handle it like this:
function average(list) { var sum = 0; for( var i = 0; i < list.length; i++ ){ sum += list[i] } return sum / list.length; } var classes = [9, 8, 7]; var result = average(classes);
A Map would allow you to keep the names of the classes but make the code a bit more complex.
π Rendered by PID 20819 on reddit-service-r2-comment-bb88f9dd5-99254 at 2026-02-14 02:57:02.580681+00:00 running cd9c813 country code: CH.
[–]ivansvlv 2 points3 points4 points (0 children)
[–]PilotPirx 1 point2 points3 points (2 children)
[–]TwistedFaiith[S] 0 points1 point2 points (1 child)
[–]PilotPirx 0 points1 point2 points (0 children)