JavaScript data structures and algorithms-array exercises

Source: Internet
Author: User

I. Create an object that records student performance, provide a way to add results, and a way to show average student performance.
// 创建一个记录学生成绩的对象const Students = function Students () {    this.grades = [];    this.addGrage = addGrade;    this.getAverage = getAverage;};// 提供一个添加成绩的方法function addGrade (grade) {    this.grades.push(grade);}// 以及一个显示学生平均成绩的方法function getAverage () {    let total = 0;    let len = this.grades.length;    while (len--) {        total += parseInt(this.grades[len], 10);    }    return total / this.grades.length;}// 示例let Mazey = new Students();Mazey.addGrage(90);Mazey.addGrage(91);Mazey.addGrage(87);Mazey.addGrage(99);Mazey.addGrage(89);Mazey.addGrage(85);Mazey.addGrage(92);console.log(Mazey.getAverage());
Two. Store a set of words in an array and display the words in both positive and reverse order.
// 将一组单词存储在一个数组中let arr = [`Mazey`, `Cherrie`, `John`, `Ada`, `Chole`];// 正序console.log(arr.sort());// 倒序console.log(arr.sort().reverse());
Three. Create an object that uses a two-dimensional array to store useful data for each month, and add methods to display the average number of average months, the average of a particular week, and the averages of all weeks.
Const MONTHDATA = function Monthdata () {//Initialize month data This.monthdata = (function () {Let arr = [];        Let week = 4;        Let day = 7;            while (week--) {Arr[week] = [];            while (day--) {Arr[week][day] = 0;        } day = 7;        } console.log (arr);    return arr;    })();    This.adddata = AddData;    This.getmonthaverage = Getmonthaverage;    This.getweekaverage = Getweekaverage; This.getallweekaverage = Getallweekaverage;}; function AddData (week, day, data) {This.monthdata[week][day] = data;}    function Getmonthaverage () {Let arr = this.monthdata, total = 0, day = 0; for (Let i = 0; i < arr.length; i++) {to (Let J = 0; J < Arr[i].length; J + +) {Total + = parseint            (Arr[i][j], 10);        day++; }} return total/day;}    function Getweekaverage (week) {Let arr = This.monthdata[week], total = 0; for (Let i = 0; i < ARR.LEngth;    i++) {Total + = parseint (Arr[i], 10); } return total/arr.length;}            function Getallweekaverage () {Let itself = this, week = this.monthData.length, total = (function () {            Let total = 0;             for (Let i = 0; i < week; i++) {Total + = Self.getweekaverage (i) * Self.monthdata[i].length}        return total;        })(); return Total/week;} Example Let Mazey = new Monthdata (); Mazey.adddata (0, 1, 100); Mazey.adddata (0, 2, 100); Mazey.adddata (0, 3, 100); Mazey.adddata (0, 4, 100); Mazey.adddata (1, 6, 7); Console.log (Mazey.getmonthaverage ()); Console.log (mazey.getweekaverage (0)); Console.log ( Mazey.getweekaverage (1)); Console.log (Mazey.getallweekaverage ());
Four. Create an object that stores the letters in an array and uses a method to connect the letters together and display them as a single word.
const Letter = function Letter () {    this.characters = [];    this.addCharacter = addCharacter;    this.toLetter = toLetter;};function addCharacter (character) {    this.characters.push(character);}function toLetter () {    return this.characters.join(``);}// 示例let Mazey = new Letter();Mazey.addCharacter(`m`);Mazey.addCharacter(`a`);Mazey.addCharacter(`z`);Mazey.addCharacter(`e`);Mazey.addCharacter(`y`);console.log(Mazey.toLetter()); // mazey

JavaScript data structures and algorithms-array exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.