JSFiddle - React, Tailwind, and code Playground

by Trevor W

JavaScript

var foo = "  320.1rr233 4567 v"

function myParseFloat(val){
  if (typeof val !== "string") {
    return NaN
  }

  val = val.trim()
  
  var newVal = 0
  var charPos = 0
  var flip = false
  var posBehind = 0
  
  while(charPos < 100){
    
    var foo = val.charAt(charPos)
    charPos++
    
    var bar = Number(foo)
    if (foo == "." && !flip){
    	flip = true
    	continue
    }
    
    if (isNaN(bar) || charPos > val.length || foo !== String(bar)) {
      return Number(newVal.toFixed(posBehind))
    }
    
    if (!flip) {
    	newVal = (newVal*10) + bar
    }else{
      multiplier = 1
      for(i = 0; i <= posBehind; i++){
      	multiplier = multiplier * 10
      }
      posBehind++
    	newVal = newVal + (bar/multiplier)
    }
    
  }
  
  return Number(newVal.toFixed(posBehind))
}

console.log("Orig:",typeof foo,foo);
console.log("Mine:",typeof myParseFloat(foo),myParseFloat(foo));
console.log("Norm:",typeof parseFloat(foo),parseFloat(foo))