// check.js /** * This script provides various utility functions to check different conditions. */ // Function to check if a number is prime function isPrime(num) { if (num <= 1) return false; if (num <= 3) return true; if (num % 2 === 0 || num % 3 === 0) return false; for (let i = 5; i * i <= num; i += 6) { if (num % i === 0 || num % (i + 2) === 0) return false; } return true; } // Function to check if a string is a palindrome function isPalindrome(str) { const cleanedStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); const reversedStr = cleanedStr.split('').reverse().join(''); return cleanedStr === reversedStr; } // Function to check if an email address is valid function isValidEmail(email) { const emailRegex = /\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } // Function to check if a URL is valid function isValidURL(url) { const urlRegex = /^(https?|ftp):\/\/[^\s$.?#].[^\s]*$/; return urlRegex.test(url); } // Example usage of the functions console.log(isPrime(7)); // true console.log(isPrime(4)); // false console.log(isPalindrome("racecar")); // true console.log(isPalindrome("hello")); // false console.log(isValidEmail("test@example.com")); // true console.log(isValidEmail("invalid-email")); // false console.log(isValidURL("https://www.example.com")); // true console.log(isValidURL("invalid-url")); // false /* FAQs */ /** * How do I check if a number is prime using this script? * * You can use theisPrime
function to check if a number is prime. Simply pass the number as an argument to the function and it will returntrue
if the number is prime andfalse
otherwise. For example: * * console.log(isPrime(7)); // true * console.log(isPrime(4)); // false */ /** * What are the requirements for a valid email address according to this script? * * According to theisValidEmail
function in this script, a valid email address must: * Contain exactly one "@" symbol. * Have characters before and after the "@" symbol. * End with a period followed by at least one character. * * The regular expression used in the function is: * const emailRegex = /\s@]+@[^\s@]+\.[^\s@]+$/; * This ensures that the email meets the basic structure requirements for a valid email address.
以上就是关于“check.js”的问题,朋友们可以点击主页了解更多内容,希望可以够帮助大家!
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1410931.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复