Hướng dẫn is there a sum method in javascript? - có một phương thức tổng trong javascript?

Hướng dẫn is there a sum method in javascript? - có một phương thức tổng trong javascript?
Abel Lifaefi Mbula

Trong bức ảnh này, chúng tôi sẽ thảo luận về ba phương pháp bạn có thể sử dụng để có được tổng số một mảng nhất định trong JavaScript.

Đầu tiên, chúng tôi sẽ sử dụng vòng lặp truyền thống. Thứ hai, chúng tôi sẽ sử dụng ____10, một phương pháp giống như mảng và cuối cùng, chúng tôi sẽ sử dụng

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

1.

Trong ảnh này, ví dụ mảng của chúng tôi là

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

2 và đầu ra dự kiến ​​là

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

3.

1. Sử dụng vòng lặp for truyền thống

Trong phương thức này, bạn lặp lại và thêm từng mục cho đến khi bạn đạt được mục cuối cùng.

function sumArray(array){
  let sum = 0 // the sum is initialed to 0

/* js arrays are zero-index based
ourArray.length = 5, the initialization block is set to 0.
the last item is index 4 that is < 5 (what we define in the condition block)
*/
  for (let i = 0; i < 
  array.length; i += 1) {
  // take every item in the array and add it to sum variable
  sum += array[i]
  // initial: sum = 0 
  // iteration 1: 0 + 1 => sum = 1
  // iteration 2: 1 + 4 => sum = 5
  // iteration 3: 5 + 0 => sum = 5
  // iteration 4: 5 + 9 => sum = 14
  // iteration 5: 14 + -3 => sum = 11

  }

  console.log(sum) // 11
  // return sum
  return sum
}

// call the function and give it our array
sumArray([1, 4, 0, 9, -3]); // logs 11

Mã không có bình luận

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

2. Sử dụng function sumArray(array) { const ourArray = [1, 4, 0, 9, -3]; let sum = 0; for (let i = 0; i < ourArray.length; i += 1) { sum += ourArray[i]; } (adsbygoogle = window.adsbygoogle || []).push({}); return sum;}console.log(sumArray([1, 4, 0, 9, -3]));0

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

0 là một phương pháp tích hợp hoặc giống như mảng. Nó rất đơn giản, như hình dưới đây. is a built-in or array-like method. It’s simple, as shown below.

function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11

Như bạn có thể thấy, giải pháp ngắn và sạch sẽ.

Mã không có bình luận

function sumArray(array) {

let sum = 0;

array.forEach(item => {

sum += item;

});

console.log(sum);

return sum;

}

sumArray([1, 4, 0, 9, -3]);

2. Sử dụng function sumArray(array) { const ourArray = [1, 4, 0, 9, -3]; let sum = 0; for (let i = 0; i < ourArray.length; i += 1) { sum += ourArray[i]; } return sum;}console.log(sumArray([1, 4, 0, 9, -3]));0

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

0 là một phương pháp tích hợp hoặc giống như mảng. Nó rất đơn giản, như hình dưới đây. lets us iterate over an array, so we can make use of it to get the sum of an array. Since we keep the same style of functions as above, let’s copy and adjust.

function sumArray(array) {
  let sum = 0; 

  /*loop over array and add each item to sum
  */
  for (const item of array) {
    sum += item;
  }
 
 // return the result 
  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11

Như bạn có thể thấy, giải pháp ngắn và sạch sẽ.

function sumArray(array) {

let sum = 0;

/*loop over array and add each item to sum

*/

for (const item of array) {

sum += item;

}

// return the result

console.log(sum);

return sum;

}

sumArray([1, 4, 0, 9, -3]); //logs 11

3. Sử dụng

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

1

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

1 cho phép chúng tôi lặp lại một mảng, vì vậy chúng tôi có thể sử dụng nó để có được tổng của một mảng. Vì chúng tôi giữ cùng một kiểu chức năng như trên, hãy để sao chép và điều chỉnh.

Chúng tôi vẫn nhận được kết quả tương tự như các phương pháp khác mà chúng tôi đã sử dụng. Bạn có thể kiểm tra nó trên tiện ích dưới đây.

THẺ LIÊN QUAN

Abel Lifaefi Mbula

Nhận tổng của một mảng số #

Để có được tổng của một mảng số:

  1. Sử dụng phương thức

    function sumArray(array) {

    const ourArray = [1, 4, 0, 9, -3];

    let sum = 0;

    for (let i = 0; i < ourArray.length; i += 1) {

    sum += ourArray[i];

    }

    return sum;

    }

    console.log(sumArray([1, 4, 0, 9, -3]));

    9 để lặp qua mảng.
  2. Đặt giá trị ban đầu trong phương thức
    function sumArray(array) {
      let sum = 0; 
    
    /*Go through each item in the array and execute this function which adds
    each item to sum 
    */
      array.forEach(item => {
        sum += item;
      });
    
      console.log(sum); 
      return sum;
    }
    
    sumArray([1, 4, 0, 9, -3]); //logs 11
    
    0 thành
    function sumArray(array) {
      let sum = 0; 
    
    /*Go through each item in the array and execute this function which adds
    each item to sum 
    */
      array.forEach(item => {
        sum += item;
      });
    
      console.log(sum); 
      return sum;
    }
    
    sumArray([1, 4, 0, 9, -3]); //logs 11
    
    1.
  3. Trên mỗi lần lặp, trả về tổng giá trị tích lũy và số hiện tại.

Copied!

const arr = [5, 15, 45]; const sum = arr.reduce((accumulator, value) => { return accumulator + value; }, 0); console.log(sum); // 👉️ 65

Tham số

function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11
2 ban đầu được đặt thành
function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11
1 bởi vì đó là những gì chúng tôi đã chuyển làm đối số thứ hai cho phương thức giảm.

Trên mỗi lần lặp, chúng tôi trả về tổng của

function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11
2 và phần tử mảng hiện tại.

Một cách tiếp cận khác và có lẽ đơn giản hơn là sử dụng vòng lặp

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

1.

Để có được tổng của một mảng số:

  1. Sử dụng phương thức

    function sumArray(array) {

    const ourArray = [1, 4, 0, 9, -3];

    let sum = 0;

    for (let i = 0; i < ourArray.length; i += 1) {

    sum += ourArray[i];

    }

    return sum;

    }

    console.log(sumArray([1, 4, 0, 9, -3]));

    9 để lặp qua mảng.
  2. Đặt giá trị ban đầu trong phương thức
    function sumArray(array) {
      let sum = 0; 
    
    /*Go through each item in the array and execute this function which adds
    each item to sum 
    */
      array.forEach(item => {
        sum += item;
      });
    
      console.log(sum); 
      return sum;
    }
    
    sumArray([1, 4, 0, 9, -3]); //logs 11
    
    0 thành
    function sumArray(array) {
      let sum = 0; 
    
    /*Go through each item in the array and execute this function which adds
    each item to sum 
    */
      array.forEach(item => {
        sum += item;
      });
    
      console.log(sum); 
      return sum;
    }
    
    sumArray([1, 4, 0, 9, -3]); //logs 11
    
    1.
  3. Trên mỗi lần lặp, trả về tổng giá trị tích lũy và số hiện tại.

Copied!

const arr = [5, 15, 45]; let sum = 0; for (const value of arr) { sum += value; } console.log(sum); // 👉️ 65

Tham số

function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11
2 ban đầu được đặt thành
function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11
1 bởi vì đó là những gì chúng tôi đã chuyển làm đối số thứ hai cho phương thức giảm.

Trên mỗi lần lặp, chúng tôi trả về tổng của

function sumArray(array) {
  let sum = 0; 

/*Go through each item in the array and execute this function which adds
each item to sum 
*/
  array.forEach(item => {
    sum += item;
  });

  console.log(sum); 
  return sum;
}

sumArray([1, 4, 0, 9, -3]); //logs 11
2 và phần tử mảng hiện tại.

Một cách tiếp cận khác và có lẽ đơn giản hơn là sử dụng vòng lặp

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

1.

Bạn cũng có thể sử dụng vòng lặp for cơ bản để tổng hợp một mảng số.

Copied!

const arr = [5, 15, 45]; let sum = 0; for (let index = 0; index < arr.length; index++) { sum += arr[index]; } console.log(sum); // 👉️ 65

Ví dụ này đạt được cùng một mục tiêu, nhưng thay vì sử dụng

function sumArray(array) {

const ourArray = [1, 4, 0, 9, -3];

let sum = 0;

for (let i = 0; i < ourArray.length; i += 1) {

sum += ourArray[i];

}

return sum;

}

console.log(sumArray([1, 4, 0, 9, -3]));

1, chúng tôi đã sử dụng vòng lặp for cơ bản.

Có một tổng () trong javascript?

Hàm sum () trong d3.js được sử dụng để trả về tổng của các phần tử của mảng đã cho.Nếu mảng trống thì nó sẽ trả về 0. tham số: Hàm này chấp nhận một mảng tham số là một mảng các phần tử có tổng số sẽ được tính toán. js is used to return the sum of the given array's elements. If the array is empty then it returns 0. Parameters: This function accepts a parameters Array which is an array of elements whose sum are to be calculated.

Làm thế nào để bạn tổng số trong JavaScript?

const num1 = parseInt (nhắc ('nhập số đầu tiên'));const num2 = parseInt (nhắc ('nhập số thứ hai'));Sau đó, tổng của các số được tính toán.const sum = num1 + num2;Cuối cùng, tổng được hiển thị.const sum = num1 + num2; Finally, the sum is displayed.

Làm thế nào để bạn tổng hợp một mảng trong JavaScript?

Để có được tổng của một mảng số: sử dụng mảng.Giảm () phương thức để lặp qua mảng.Đặt giá trị ban đầu trong phương thức giảm thành 0.Trên mỗi lần lặp, trả về tổng giá trị tích lũy và số hiện tại.Use the Array. reduce() method to iterate over the array. Set the initial value in the reduce method to 0 . On each iteration, return the sum of the accumulated value and the current number.

Bạn có thể tổng hợp một mảng không?

Để tìm tổng của tất cả các phần tử trong một mảng, chúng ta chỉ có thể lặp lại mảng và thêm từng phần tử vào một biến tích lũy tổng.iterate the array and add each element to a sum accumulating variable.