Added a New Project: 3D Distance Calculator · Nash2610/30DaysOfJavaScript@4a41ef2 · GitHub
Skip to content

Commit 4a41ef2

Browse files
committed
Added a New Project: 3D Distance Calculator
1 parent c36c055 commit 4a41ef2

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function calculate() {
2+
// Get input values
3+
var x1 = parseFloat(document.getElementById('x1').value);
4+
var y1 = parseFloat(document.getElementById('y1').value);
5+
var z1 = parseFloat(document.getElementById('z1').value);
6+
var x2 = parseFloat(document.getElementById('x2').value);
7+
var y2 = parseFloat(document.getElementById('y2').value);
8+
var z2 = parseFloat(document.getElementById('z2').value);
9+
10+
// Check if input values are valid
11+
if (isNaN(x1) || isNaN(y1) || isNaN(z1) || isNaN(x2) || isNaN(y2) || isNaN(z2)) {
12+
document.getElementById('result').innerHTML = 'Please enter valid numbers for both points.';
13+
return;
14+
}
15+
16+
// Calculate distance
17+
var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1, 2));
18+
19+
// Display the result
20+
document.getElementsByClassName('display')[0].innerHTML = '<span>|AB| = </span>' + distance.toFixed(3);
21+
}
Lines changed: 73 additions & 0 deletions

‎30DaysOfJavaScript/assets/118.png‎

22.2 KB
Loading

0 commit comments

Comments
 (0)