DeveloperBreeze

Calculate Distance Between Two Points

// Function to calculate the distance between two points in a 2D plane
function calculateDistance(x1, y1, x2, y2) {
    const deltaX = x2 - x1;
    const deltaY = y2 - y1;
    return Math.sqrt(deltaX ** 2 + deltaY ** 2);
}

// Calculate and log the distance between two points (e.g., (0, 0) and (3, 4))
const distance = calculateDistance(0, 0, 3, 4);
console.log('Distance Between Two Points:', distance);

Related Posts

More content you might like

Tutorial
javascript

Running JavaScript in the Browser Console

  • Open the browser.
  • Right-click on the webpage and select Inspect or press Ctrl+Shift+I (Cmd+Option+I on macOS).
  • Go to the Console tab.
  • Open the browser.
  • Right-click on the webpage and select Inspect or press Ctrl+Shift+K (Cmd+Option+K on macOS).
  • Navigate to the Console tab.

Dec 10, 2024
Read More
Tutorial
javascript

Mastering console.log Advanced Usages and Techniques

console.count keeps track of how many times it has been called with a particular label, which can be useful for debugging loops or recursive functions.

function recursiveFunction(num) {
  console.count('Recursive function called');
  if (num > 0) recursiveFunction(num - 1);
}
recursiveFunction(5);
// Output: Recursive function called: 1, 2, 3, 4, 5, 6

Sep 02, 2024
Read More
Code
javascript

Parse JSON String to Object

// Example JSON string
const jsonString = '{"name":"John","age":30,"city":"New York"}';

// Parse the JSON string into a JavaScript object
const parsedObject = JSON.parse(jsonString);

// Log the parsed object to the console
console.log('Parsed Object:', parsedObject);

Jan 26, 2024
Read More
Code
javascript

Validate Password Strength

// Function to check if a password meets certain strength criteria
function isValidPassword(password) {
    // Customize validation rules as needed
    const minLength = 8;
    const hasUppercase = /[A-Z]/.test(password);
    const hasLowercase = /[a-z]/.test(password);
    const hasDigit = /\d/.test(password);

    // Check if the password meets the criteria
    return password.length >= minLength && hasUppercase && hasLowercase && hasDigit;
}

// Example password
const password = 'SecurePwd123';

// Log whether the password is valid to the console
console.log('Is Valid Password:', isValidPassword(password));

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!