taesik

Coordinates Validator 본문

functions and trick/typescript_javascript

Coordinates Validator

taesikk 2022. 1. 28. 17:07

Valid coordinates look like the following: "23.32353342, -32.543534534". The return value should be either true or false.

Latitude (which is first float) can be between 0 and 90, positive or negative. Longitude (which is second float) can be between 0 and 180, positive or negative.

Coordinates can only contain digits, or one of the following symbols (including space after comma) __ -, . __

 

 

export function isValidCoordinates(coordinates:string):boolean{
  const pattern = /^-?(90|[0-8]\d|\d)(?:\.\d*)?,\s-?(1[0-7]\d|180|\d{1,2})(?:\.\d*)?$/
  return pattern.test(coordinates);
}
export function isValidCoordinates(coordinates: string): boolean {
    const [lat, lng] = coordinates.split(', ').map(Number);
    return lat > -90 && lat < 90 && lng > -180 && lng < 180;
}

'functions and trick > typescript_javascript' 카테고리의 다른 글

Consecutive strings  (0) 2022.01.29
digitRoot  (0) 2022.01.29
Count Friday 13th given year  (0) 2022.01.28
Tribonacci  (0) 2022.01.28
temp  (0) 2022.01.26