简体   繁体   中英

Convert feet in 5'5" notation to cm with JavaScript

I have a lot of heights, in inches, which need to be checked if they are between other heights. However, the data isn't 100% perfect; sometimes the number appears as 5'5" and sometimes 5' 5" (with a space). The height that it appears between will also be varied, looking sometimes like 5'5" - 5'10" and sometimes like 5' 5"-5' 10" and sometimes like 5'5"-5' 10" Height ...you get the idea.

Thus, I am trying to build a function that will take the number like 5'5" and confirm either true or false that the number is between a height range in the format 5' 5"-5' 10" .

function checkHeight(userHeight) {

  var rex = /^(\d+)'(\d+)(?:''|")$/;
  var match = rex.exec(userHeight);
  var feet, inch;

  if (match) {
    feet = parseInt(match[1], 10);
    inch = parseInt(match[2], 10);
    console.log("feet = " + feet + ", inches = " + inch);
  } else {
    console.log("Didn't match");
  }

};

checkHeight("5' 5\"")

This uses a similar regex, but removes the ^ and $ so the match can occur anywhere and adds a \\s* so that there can be any amount of whitespace between the feet and inches. It also does the test in a simpler way and avoids converting to cm altogether:

 function checkHeight( userHeightStr, heightRangeStr ) { const [ userHeight, minHeight, maxHeight ] = [ userHeightStr, ...heightRangeStr.split('-') ].map( heightStr => { const [ , feet, inches ] = heightStr.match( /(\\d+)'\\s*(\\d+)(?:''|")/ ); return feet*12 + +inches; } ) ; console.log( 'Heights in inches: ', { userHeight, minHeight, maxHeight } ); return minHeight <= userHeight && userHeight <= maxHeight; } console.log( checkHeight("5' 4\\"","5' 5\\" - 6' 1\\" Height") ); // false console.log( checkHeight("5' 5\\"","5'5\\" - 6' 1\\" Height") ); // true console.log( checkHeight("5'10\\"","5'5\\"-5' 10\\"") ); // true console.log( checkHeight("5'11\\"","5'5\\" -5' 10''") ); // false 

I found this, using the suggested regex at this link .

  function checkHeight(userHeight,heightRange) { var heightRangeArr = heightRange.replace(/\\s/g,'').toLowerCase().replace('height','').split('-'); var heightRangeStart = heightRangeArr[0]; var heightRangeFinish = heightRangeArr[1]; var userHeight = userHeight.replace(/\\s/g,''); if (convertToCm(userHeight) >= convertToCm(heightRangeStart) && convertToCm(userHeight) <= convertToCm(heightRangeFinish) ) { console.log(true); } else { console.log(false); } }; function convertToCm(inchInput) { var rex = /^(\\d+)'(\\d+)(?:''|")$/; var match = rex.exec(inchInput); var feet, inch; if (match) { feet = parseInt(match[1], 10); inch = parseInt(match[2], 10); } else { console.log("Didn't match"); } cmConvert = ((feet * 12) + inch)*2.54; console.log('*** *** ***'); console.log(cmConvert); return cmConvert; } checkHeight("5' 5\\"","5'5\\"-5' 10\\" Height"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM