Created
March 16, 2023 18:53
-
-
Save ramazansancar/ea53fff80587677a88f52c594d3ea89f to your computer and use it in GitHub Desktop.
CodeWars Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://www.codewars.com/kata/515decfd9dcfc23bb6000006/solutions/javascript | |
const isValidIP = (str) => { | |
const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)'; | |
const regex = new RegExp(`^${octet}\\.${octet}\\.${octet}\\.${octet}$`); | |
return (regex.test(str)) ? true : false | |
} // Source: https://stackoverflow.com/a/54742549/15030183 | |
function isValidIP(str) { | |
return /^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])(\.(?!$)|$)){4}$/.test(str); | |
} | |
const isValidIP = require('net').isIPv4; | |
function isValidIP(str) { | |
return str.split('.').filter(function(v) {return +v <= 255 && +v >= 0 && v.length == String(+v).length;}).length == 4; | |
} | |
const isValidIP = (str) => /^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/.test(str); | |
function isValidIP(str) { | |
return /^((([1-9]|1\d|2[0-4])?\d|25[0-5])\.){3}(([1-9]|1\d|2[0-4])?\d|25[0-5])$/.test(str); | |
} | |
function isValidIP(str) { | |
return /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$/.test(str); | |
} | |
function isValidIP(str) { | |
const validIP = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi; | |
return validIP.test(str); | |
} | |
const isValidIP = str => str.split('.').length === 4 && str.split('.').map(arr => (+arr >= 0 && +arr <= 255 && `${parseInt(arr, 10)}` === arr)).every(Boolean); | |
const isValidIP = str => /^(?:\d+\.){3}\d+$/.test(str) && str.match(/\d+/g).every(group => group < 256 && !/^0\d/.test(group)); | |
// ---- // | |
function isValidIPv4(num) { | |
return (+num >= 0) && (+num <= 255) && (/^\d{1,3}$/.test(num)) && !(/^0/.test(num) && num.length > 1); | |
} | |
function isValidIP(str) { | |
return str.split(".").filter(isValidIPv4).length === 4; | |
} | |
// ---- // | |
function isValidIP(str) { | |
let strArray = str.split("."); | |
let temp; | |
let invalidChar = ['\n',' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','/','-','+']; | |
if (strArray.length != 4) { | |
return false; | |
} else { | |
for (let i = 0; i < strArray.length; i++) { | |
temp = parseInt(strArray[i]); | |
if (!(temp >= 0 && temp <= 255)){ | |
return false; | |
} | |
for (let j = 0; j < strArray[i].length; j++){ | |
if (strArray[i][0] == "0" && strArray[i].length > 1){ | |
return false; | |
} | |
else if (invalidChar.includes(strArray[i][j])){ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
} | |
function isValidIP(str) { | |
let isValid = true | |
if (!str){ | |
return false | |
} | |
let toArr = str.split("") | |
let groupOne = [] | |
let groupTwo = [] | |
let groupThree = [] | |
let groupFour = [] | |
let temp = [] | |
let isOneFilled = false | |
let isTwoFilled = false | |
let isThreeFilled = false | |
let isFourFilled = false | |
for(let i=0; i<toArr.length; i++){ | |
if(toArr[i] !== "." && i !== toArr.length -1){ | |
if(toArr[i] == " "||toArr[i] === "e"||toArr[i] === "\n"){ | |
isValid = false | |
} | |
temp.push(toArr[i]) | |
} else if(i == toArr.length -1){ | |
if(toArr[i] == " "||toArr[i] === "e"||toArr[i] === "\n"){ | |
isValid = false | |
} | |
temp.push(toArr[i]) | |
if(!isFourFilled){ | |
groupFour = temp | |
temp = [] | |
isFourFilled = true | |
} else { | |
isValid = false | |
} | |
} else{ | |
if(!isOneFilled){ | |
groupOne = temp | |
temp=[] | |
isOneFilled = true | |
} else if(!isTwoFilled){ | |
groupTwo = temp | |
temp=[] | |
isTwoFilled = true | |
} else if(!isThreeFilled){ | |
groupThree = temp | |
temp=[] | |
isThreeFilled = true | |
} else if(!isFourFilled){ | |
groupFour= temp | |
temp=[] | |
isFourFilled = true | |
} else { | |
isValid = false | |
} | |
} | |
} | |
if(groupOne.length == 0|| groupTwo.length == 0|| groupThree.length == 0|| groupFour.length == 0){ | |
isValid = false | |
} | |
if ( | |
(groupOne.length>1 && groupOne[0]==0 ) || (groupTwo.length>1 && groupTwo[0]==0 ) ||(groupThree.length>1 && groupThree[0]==0 ) ||(groupFour.length>1 && groupFour[0]==0) | |
){ | |
console.log('d'); | |
isValid = false | |
} | |
if (groupOne==[0]|| groupTwo==[0] || groupThree==[0] || groupFour==[0]){ | |
console.log('kena'); | |
isValid = true | |
} | |
let str0ne = groupOne.join("") | |
if((str0ne) > 255 || isNaN(str0ne) || str0ne<0){ | |
isValid = false | |
} | |
let strTwo = groupTwo.join("") | |
if(strTwo > 255 || isNaN(strTwo) || strTwo<0){ | |
isValid = false | |
} | |
let strThree = groupThree.join("") | |
if(strThree > 255 || isNaN(strThree) || strThree<0){ | |
isValid = false | |
} | |
let strFour = groupFour.join("") | |
strFour = +strFour | |
if(strFour > 255 || isNaN(strFour) || strFour<0){ | |
isValid = false | |
} | |
return isValid | |
} | |
function isValidIP(str) { | |
return /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/g.test(str); | |
} | |
isValidIP=a=>/^((1\d\d?|[1-9]\d|\d|2[0-4]\d|25[0-5])\.){3}(1\d\d?|[1-9]\d|\d|2[0-4]\d|25[0-5])$/.test(a) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment