6

Hello All I have array & i need to perform various operation like sum, total, average. All these 3 are achieved, Now I need to find the minimum & maximum value in array. I am stucked on this below is the code.

Below is TS Part

people: Array<number> = [1, 2, 3, 4, 5];
  total: number = 0;
  arrayLength: number = this.people.length;
  average: number = 0;

  sum() {
    for (var i in this.people) { this.total += this.people[i]; }
  }

  ngOnInit() {
    this.sum();
    this.average = (this.total / this.arrayLength);
  }

Below is HTML Part

<span *ngFor="let p of people" style="font-size:18px">{{p}} </span><br><br>
<button >Quantity</button> = {{arrayLength}}<Br><br>
<button >Average</button> = {{average}}<Br><br>
<button >Sum</button> <span *ngIf="sumShow"> = {{total}}</span><Br><br>

5 Answers 5

10

use reduce for this.

Demo on Stackblitz

  sum() {
    this.total = this.people.reduce((a, b)=>a + b); 
  }

  ngOnInit() {
    this.sum();
    this.max = this.people.reduce((a, b)=>Math.max(a, b)); 
    this.min = this.people.reduce((a, b)=>Math.min(a, b)); 
    this.average = (this.total / this.arrayLength);
  }


<span *ngFor="let p of people" style="font-size:18px">{{p}} </span><br><br>
<button >Quantity</button> = {{arrayLength}}<Br><br>
<button >Average</button> = {{average}}<Br><br>
<button >Sum</button> <span > = {{total}}</span><Br><br>

<button >Max</button> <span > = {{max}}</span><Br><br>
<button >Min</button> <span > = {{min}}</span><Br><br>
Sign up to request clarification or add additional context in comments.

7 Comments

Brother i need to display two values minimum which will be 1 in my case & maximum value will be 5
Can we calculate Standard Deviation on the same array ?
ohh that would be very helpfull. If we use the same array standard deviation value will be 1.414214
OK brother take your time. Let me know once its done
Hi @JigneshMistry I have update link with Standard Deviation for calculate average.
|
10

Use Math.max and Math.min combined with the spread operator.

get max() {
  return Math.max(...this.people);
}

get min() {
  return Math.min(...this.people);
}

Comments

4

You could create yourself a little helper class that does those operations for you and is reusable throughout your code

export class MathOps {
  array: number[];

  constructor(array: number[]) {
    this.array = array;
  }

  sum(): number {
    return this.array.reduce((a, b) => a + b, 0);
  }

  avg(): number {
    return this.sum() / this.array.length;
  }

  max(): number {
    return Math.max(...this.array);
  }

  min(): number {
    return Math.min(...this.array);
  }
}

const ops = new MathOps([1, 2, 3, 4, 5]);
console.log(ops.avg());
console.log(ops.max());
console.log(ops.min());
console.log(ops.sum());

Note:

Depending on use case, you'll want to extend this to cache results...

1 Comment

Brilliant that solved all my problems Great Help Brother
1

You can use Array.reduce and Math.max() , Math.min() for this.

const people = [1,2,3,4,5];

const max = people.reduce((a, b) => Math.max(a, b));  // 5

const min = people.reduce((a, b) => Math.min(a, b));  // 1

const sum = people.reduce((a, b) => a+b, 0);  // 15

And you can find a working example in here

1 Comment

Thank You Buddy :)
0

For array of object (Min & Max) :

const arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: -1, b: -2, c: -3}];
const min = Math.min(...arr.map(obj => obj.a + obj.b + obj.c));
const max = Math.max(...arr.map(obj => obj.a + obj.b + obj.c));
const minResult = arr.find(obj => obj.a + obj.b + obj.c == min); // {a: -1, b: -2, c: -3}
const maxResult = arr.find(obj => obj.a + obj.b + obj.c == max); // {a: 4, b: 5, c: 6}

[OR]

const arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: -1, b: -2, c: -3}];
const min = arr.reduce((p,v) => (p.a + p.b + p.c) < (v.a + v.b + v.c) ? p : v); // {a: -1, b: -2, c: -3}
const max = arr.reduce((p,v) => (p.a + p.b + p.c) > (v.a + v.b + v.c) ? p : v); // {a: 4, b: 5, c: 6}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.