Implementation steps: 1. Create an array containing multiple data, the syntax "var arr=[num1,num2..numN];"; 2. Use forEach() to calculate the sum of the array elements, the syntax "var sum=0;function f(v){sum =v;}arr.forEach(f);"; 3. Use the length attribute to calculate the array length, the syntax is "arr.length;"; 4. Divide the total score by the array length You can get the average value with the syntax "sum/len".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, you can use arrays, the forEach() method and the length attribute to find the total score and average.
Implementation steps:
Step 1: Create an array containing multiple numeric data
var 数组名=[数值1,数值2..,数值N];
Step 2: Use forEach() to iterate the array to calculate the sum of the array elements (total score)
var sum = 0; function f(value) { sum += value; } 数组对象.forEach(f);
Step 3: Use the length attribute to calculate the length of the array
var len=数组对象.length;
Step 4: Divide the total score by the array length
var avg=sum/len;
Implementation example:
var a = [10, 11, 12], sum = 0,len,avg; console.log(a); function f(value) { sum += value; } a.forEach(f); console.log("数组元素总和为:"+sum); len=a.length; console.log("数组长度为:"+len); avg=sum/len; console.log("数组平均数为:"+avg);
Description:
1. forEach() method
forEach() method is used to call Each element of the array and passes the element to the callback function.
array.forEach(funtion callbackfn(value, index, array), thisValue)
funtion callbackfn(value, index, array)
: Required parameters, specify the callback function, can receive up to three parameters:
value : The value of the array element.
index: Numeric index of the array element.
array: Array object containing the element.
thisValue
: an omitted parameter, an object that can be referenced by this in the callback function. If thisArg is omitted, the value of this is undefined.
2. The length attribute
The length attribute can set or return the number of elements in the array.
Syntax
Set the number of arrays:
array.length=number
Return the number of arrays:
array.length
Return value:
A number indicating the number of elements of the object in the array.
【Related recommendations: javascript video tutorial, programming video】
The above is the detailed content of How to find the total score and average in javascript. For more information, please follow other related articles on the PHP Chinese website!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

HTML5isnotinherentlyinsecure,butitsfeaturescanleadtosecurityrisksifmisusedorimproperlyimplemented.1)Usethesandboxattributeiniframestocontrolembeddedcontentandpreventvulnerabilitieslikeclickjacking.2)AvoidstoringsensitivedatainWebStorageduetoitsaccess

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
