TZinovieva

Points Validation JS Fundamentals

Feb 5th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isValidDistance(points) {
  2.   for (let i = 0; i < 3; i++) {
  3.     let x1, y1, x2, y2;
  4.     if (i === 0) {
  5.       x1 = points[0];
  6.       y1 = points[1];
  7.       x2 = 0;
  8.       y2 = 0;
  9.     } else if (i === 1) {
  10.       x1 = points[2];
  11.       y1 = points[3];
  12.       x2 = 0;
  13.       y2 = 0;
  14.     } else {
  15.       x1 = points[0];
  16.       y1 = points[1];
  17.       x2 = points[2];
  18.       y2 = points[3];
  19.     }
  20.     let distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  21.     if (distance % 1 === 0) {
  22.       console.log(`{${x1}, ${y1}} to {${x2}, ${y2}} is valid`);
  23.     } else {
  24.       console.log(`{${x1}, ${y1}} to {${x2}, ${y2}} is invalid`);
  25.     }
  26.   }
  27. }
  28.  
  29. isValidDistance([3, 0, 0, 4]);
  30. isValidDistance([2, 1, 1, 1]);
  31.  
Advertisement
Add Comment
Please, Sign In to add comment