Google has a unit conversion feature that takes a human readable string such as:
1m to feet
and uses this as an unit conversion / calculator instruction. it also supports calculations on left side of the ‘to keyword’.
1 * (2 * 10) / 2m to cm
How would one go about implimenting a simular string interpretation in javascript?
EDIT added some sample code:
function UnitQuery(str) {
var arr = str.toLowerCase().split(' '),
len = arr.length,
afterToKeyword = false,
queryObj = {
left : [],
right : []
},
current = queryObj.left;
for ( i = 0; i < len; i++ ) {
if (!afterToKeyword && arr[i].search('to') >= 0){
afterToKeyword = true;
current = queryObj.right;
}
current.push( arr[i] );
}
return queryObj;
}
console.info( UnitQuery("1 m to feet") );
Start by tokenizing the input .split() then taking action on the values.