Я выполняю следующее упражнение JS, в котором мне нужно проанализировать все числа в данном абзаце, а затем просуммировать все эти числа.
function get_sum() {
let s = document.getElementById('pc').textContent;
let matches = s.match(/(\d+)/);
let sum = 0;
for(let i = 0; i < matches.length; ++i) {
sum += matches[i];
}
console.log(sum);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PC</title>
</head>
<body>
<p id="pc"> The cost of the processor is 9000.
The cost of the motherboard is 15000. The memory card is 6000.
The price of the monitor is 7000. The hard disk price is 4000.
Other item's cost is 6000. </p>
<button type="button" onclick='get_sum()'>Get Sum</button>
</body>
</html>
Результат должен быть оценкой выражения 9000 + 15000 + 6000 + 7000 + 4000 + 6000, т.е. 47000