Hướng dẫn how do you get the sum of a number in javascript? - làm thế nào để bạn có được tổng của một số trong javascript?

Các số nguyên dương 1, 2, 3, ... được gọi là số tự nhiên.1, 2, 3, ... are known as natural numbers.

Ví dụ 1: Tổng số tự nhiên sử dụng cho vòng lặp

// program to display the sum of natural numbers

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

let sum = 0;

// looping from i = 1 to number
// in each iteration, i is increased by 1
for (let i = 1; i <= number; i++) {
    sum += i;
}

console.log('The sum of natural numbers:', sum);

Đầu ra

Enter a positive integer: 100
The sum of natural numbers: 5050

Trong chương trình trên, người dùng được nhắc nhập một số.

Enter a positive integer: 100
The sum of natural numbers: 5050
7 chuyển đổi giá trị chuỗi số thành giá trị số nguyên.

Vòng lặp

Enter a positive integer: 100
The sum of natural numbers: 5050
8 được sử dụng để tìm tổng số tự nhiên lên đến số do người dùng cung cấp.

  • Giá trị của tổng là 0 ban đầu.0 initially.
  • Sau đó, một vòng
    Enter a positive integer: 100
    The sum of natural numbers: 5050
    8 được sử dụng để lặp lại từ
    // program to display the sum of natural numbers
    
    // take input from the user
    const number = parseInt(prompt('Enter a positive integer: '));
    
    let sum = 0, i = 1;
    
    // looping from i = 1 to number
    while(i <= number) {
        sum += i;
        i++;
    }
    
    console.log('The sum of natural numbers:', sum);
    0.
  • Trong mỗi lần lặp, tôi được thêm vào tổng và giá trị của
    // program to display the sum of natural numbers
    
    // take input from the user
    const number = parseInt(prompt('Enter a positive integer: '));
    
    let sum = 0, i = 1;
    
    // looping from i = 1 to number
    while(i <= number) {
        sum += i;
        i++;
    }
    
    console.log('The sum of natural numbers:', sum);
    1 được tăng thêm 1.1.
  • Khi tôi trở thành 101, điều kiện thử nghiệm là
    // program to display the sum of natural numbers
    
    // take input from the user
    const number = parseInt(prompt('Enter a positive integer: '));
    
    let sum = 0, i = 1;
    
    // looping from i = 1 to number
    while(i <= number) {
        sum += i;
        i++;
    }
    
    console.log('The sum of natural numbers:', sum);
    2 và tổng sẽ bằng 0 + 1 + 2 + ... + 100.101, the test condition is
    // program to display the sum of natural numbers
    
    // take input from the user
    const number = parseInt(prompt('Enter a positive integer: '));
    
    let sum = 0, i = 1;
    
    // looping from i = 1 to number
    while(i <= number) {
        sum += i;
        i++;
    }
    
    console.log('The sum of natural numbers:', sum);
    2 and sum will be equal to 0 + 1 + 2 + ... + 100.

Ví dụ 2: Tổng số tự nhiên sử dụng trong khi vòng lặp

// program to display the sum of natural numbers

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

let sum = 0, i = 1;

// looping from i = 1 to number
while(i <= number) {
    sum += i;
    i++;
}

console.log('The sum of natural numbers:', sum);

Đầu ra

Enter a positive integer: 100
The sum of natural numbers: 5050

Trong chương trình trên, người dùng được nhắc nhập một số.

Enter a positive integer: 100
The sum of natural numbers: 5050
7 chuyển đổi giá trị chuỗi số thành giá trị số nguyên.

  • Vòng lặp
    Enter a positive integer: 100
    The sum of natural numbers: 5050
    8 được sử dụng để tìm tổng số tự nhiên lên đến số do người dùng cung cấp.100.
  • Giá trị của tổng là 0 ban đầu.1.
  • Khi tôi trở thành 101, điều kiện thử nghiệm là
    // program to display the sum of natural numbers
    
    // take input from the user
    const number = parseInt(prompt('Enter a positive integer: '));
    
    let sum = 0, i = 1;
    
    // looping from i = 1 to number
    while(i <= number) {
        sum += i;
        i++;
    }
    
    console.log('The sum of natural numbers:', sum);
    2 và tổng sẽ bằng 0 + 1 + 2 + ... + 100.101, the test condition is
    // program to display the sum of natural numbers
    
    // take input from the user
    const number = parseInt(prompt('Enter a positive integer: '));
    
    let sum = 0, i = 1;
    
    // looping from i = 1 to number
    while(i <= number) {
        sum += i;
        i++;
    }
    
    console.log('The sum of natural numbers:', sum);
    2 and sum will be equal to 0 + 1 + 2 + ... + 100.

22

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi là người mới.

Tôi muốn tạo ứng dụng nhỏ sẽ tính tổng của tất cả các chữ số của một số.

Ví dụ: nếu tôi có số 2568, ứng dụng sẽ tính toán 2+5+6+8 bằng với 21. Cuối cùng, nó sẽ tính tổng số của 21 chữ số và kết quả cuối cùng sẽ là 3.

Làm ơn giúp tôi

hỏi ngày 12 tháng 7 năm 2016 lúc 16:40Jul 12, 2016 at 16:40

4

Về cơ bản, bạn có hai phương pháp để có được tổng của tất cả các phần của số nguyên.

  • Với các hoạt động số

    Lấy số và xây dựng phần còn lại của mười và thêm vào đó. Sau đó lấy phần số nguyên của phân chia số vào 10. Tiến hành.

var value = 2568,
    sum = 0;

while (value) {
    sum += value % 10;
    value = Math.floor(value / 10);
}

console.log(sum);

  • Sử dụng các hoạt động chuỗi

    Chuyển đổi số thành chuỗi, chia chuỗi và nhận một mảng với tất cả các chữ số và thực hiện giảm cho mỗi phần và trả về tổng.

var value = 2568,
    sum = value
        .toString()
        .split('')
        .map(Number)
        .reduce(function (a, b) {
            return a + b;
        }, 0);

console.log(sum);


Để trả về giá trị, bạn cần thêm thuộc tính

// program to display the sum of natural numbers

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

let sum = 0, i = 1;

// looping from i = 1 to number
while(i <= number) {
    sum += i;
    i++;
}

console.log('The sum of natural numbers:', sum);
7.

rezultat.value = sum;
//      ^^^^^^

function sumDigits() {
    var value = document.getElementById("thenumber").value,
        sum = 0;

  while (value) {
      sum += value % 10;
      value = Math.floor(value / 10);
  }
  
  var rezultat = document.getElementById("result");
  rezultat.value = sum;
}




Hướng dẫn how do you get the sum of a number in javascript? - làm thế nào để bạn có được tổng của một số trong javascript?

Đã trả lời ngày 12 tháng 7 năm 2016 lúc 18:21Jul 12, 2016 at 18:21

Hướng dẫn how do you get the sum of a number in javascript? - làm thế nào để bạn có được tổng của một số trong javascript?

Nina Scholznina ScholzNina Scholz

361K24 Huy hiệu vàng324 Huy hiệu bạc365 Huy hiệu Đồng24 gold badges324 silver badges365 bronze badges

7

Làm thế nào về cách tiếp cận đơn giản này bằng cách sử dụng số học Modulo 9?

function sumDigits(n) {
  return (n - 1) % 9 + 1;
}

Đã trả lời ngày 25 tháng 11 năm 2017 lúc 13:07Nov 25, 2017 at 13:07

14

Với công thức toán học:

Enter a positive integer: 100
The sum of natural numbers: 5050
0

Không có công thức toán học:

Enter a positive integer: 100
The sum of natural numbers: 5050
1

Đã trả lời ngày 26 tháng 4 năm 2021 lúc 15:42Apr 26, 2021 at 15:42

WesleyacwesleyacWesleyAC

4945 Huy hiệu bạc10 Huy hiệu Đồng5 silver badges10 bronze badges

Tổng số chữ số có thể được tính bằng cách sử dụng hàm đó (dựa trên các câu trả lời khác):

Enter a positive integer: 100
The sum of natural numbers: 5050
2

Nếu bạn thực sự cần tổng hợp các chữ số đệ quy, có phiên bản đệ quy của hàm:

Enter a positive integer: 100
The sum of natural numbers: 5050
3

Biểu thức

// program to display the sum of natural numbers

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

let sum = 0, i = 1;

// looping from i = 1 to number
while(i <= number) {
    sum += i;
    i++;
}

console.log('The sum of natural numbers:', sum);
8 sẽ bằng
// program to display the sum of natural numbers

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

let sum = 0, i = 1;

// looping from i = 1 to number
while(i <= number) {
    sum += i;
    i++;
}

console.log('The sum of natural numbers:', sum);
9. Bởi vì
Enter a positive integer: 100
The sum of natural numbers: 5050
0 và
Enter a positive integer: 100
The sum of natural numbers: 5050
1.

Lưu ý rằng giải pháp đệ quy của @federicoanonucci sẽ hiệu quả hơn, nhưng nó không cung cấp cho bạn tổng số chữ số trung gian nếu bạn cần.

Đã trả lời ngày 30 tháng 5 năm 2020 lúc 10:32May 30, 2020 at 10:32

KonardkonardKonard

1.89823 Huy hiệu bạc20 Huy hiệu đồng23 silver badges20 bronze badges

5

Hãy thử đệ quy

Enter a positive integer: 100
The sum of natural numbers: 5050
4

Đã trả lời ngày 12 tháng 6 năm 2020 lúc 20:12Jun 12, 2020 at 20:12

Hướng dẫn how do you get the sum of a number in javascript? - làm thế nào để bạn có được tổng của một số trong javascript?

Ghen tị Hionenvy HionEnvy Hion

1641 Huy hiệu bạc8 Huy hiệu đồng1 silver badge8 bronze badges

Bạn có thể làm theo cách này.

Enter a positive integer: 100
The sum of natural numbers: 5050
5

Valor_

3.3179 Huy hiệu vàng56 Huy hiệu bạc105 Huy hiệu Đồng9 gold badges56 silver badges105 bronze badges

Đã trả lời ngày 26 tháng 8 năm 2021 lúc 4:35Aug 26, 2021 at 4:35

Hướng dẫn how do you get the sum of a number in javascript? - làm thế nào để bạn có được tổng của một số trong javascript?

1

Mở rộng theo câu trả lời của @Fethe, hàm

Enter a positive integer: 100
The sum of natural numbers: 5050
2 này có thể xử lý số lượng lớn hoặc
Enter a positive integer: 100
The sum of natural numbers: 5050
3

Enter a positive integer: 100
The sum of natural numbers: 5050
6

Đã trả lời ngày 30 tháng 7 năm 2021 lúc 4:43Jul 30, 2021 at 4:43

Hướng dẫn how do you get the sum of a number in javascript? - làm thế nào để bạn có được tổng của một số trong javascript?

Yogskiyogskiyogski

1712 Huy hiệu bạc8 Huy hiệu đồng2 silver badges8 bronze badges

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 hợp tất cả các chữ số trong một số trong JS?

Để tổng hợp tất cả các chữ số trong một số: Chuyển đổi số thành một chuỗi.Sử dụng phương thức chia () để chia chuỗi thành một mảng các chữ số.Sử dụng phương thức giảm () để tổng hợp tất cả các chữ số trong mảng.Convert the number to a string. Use the split() method to split the string into an array of digits. Use the reduce() method to sum all the digits in the array.

Làm thế nào để bạn tìm thấy tổng của một số trong java?

Tổng của hai số sử dụng các đối số dòng lệnh trong Java..
lớp công khai sumofnumbers4 ..
Công khai tĩnh chính (chuỗi args []).
int x = integer.parseInt (args [0]);// Lập luận đầu tiên ..
int y = integer.parseInt (args [1]);// đối số thứ hai ..
int sum = x + y ;.
System.out.println ("tổng của x và y là:" +sum) ;.

Làm thế nào để bạn tính toán trong JavaScript?

Chuyển đổi sang các loại dữ liệu số..
Hãy để mynumber = "74";Mynumber += 3;Bạn kết thúc với kết quả 743, không phải 77, vì Mynumber thực sự được định nghĩa là một chuỗi.....
Loại mynumber;Để sửa chữa tính toán, bạn có thể làm điều này:.
Hãy để mynumber = "74";mynumber = số (mynumber) + 3;Kết quả sau đó là 77, như dự kiến ban đầu ..