Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
Can anyone tell me why I get a error when i try to call this function?
<!doctype html>
<html>
<script>
function isNumber(value) {
    return typeof (value) != "boolean" && !isNaN(value) && value.length > 0;
}

function minMaxDefrost(value, min, max) {

    if (value.length == 0 || value == "-") return value;
    if (!isNumber(value)) return value.substring(0, value.length - 1);
    
console.log("minMaxDefrost called ")
    if (parseFloat(value) < min){
        return min;
        }
    else if (parseFloat(value) > max){
        return max;
        }
    else {
        return Math.floor(value);
    }
}


minMaxDefrost(4, 0, 12);

</script>
</html>
  • I use this function in a html keyup function and it works fine but when i try to call it by itself at the start of the code i get the error message Uncaught TypeError: value.substring is not a function
  • also not when i call this function from the keyup i use "this.value = minMaxDefrost(this.value, -80, 150, 0)
  • im assuming the error has got to do with the this.value but i dont know how to fix it
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

isNumber(4) evaluates to false, so you're trying to call a substring method on 4 but that doesn't exist. Use typeof value === 'number' instead to test if something is a number. Or better yet, use typeof value === 'string' before treating it like it's definitely a string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

isNumber doesn't exactly do its job as Mark Hanna suggests.. a better isNumber would be something like

function isNumber(num){
  let num1=num-0, num2=num-1
  return num2!=num1 && !!(num2||num1) && typeof(num)!="object"
  //Infinity would fail the test num2!=num1
  //Normal non-numbers would fail !!(num2||num1)
  //null would fail typeof(null)!="object"
  //However, 0, and even "0" will pass this test
}

Here is it returned in the code you gave us

function isNumber(num){
  let num1=num-0, num2=num-1
  return num2!=num1 && !!(num2||num1) && typeof(num)!="object"
  //Infinity would fail the test num2!=num1
  //Normal non-numbers would fail !!(num2||num1)
  //null would fail typeof(null)!="object"
  //However, 0, and even "0" will pass this test
}

function minMaxDefrost(value, min, max) {

    if (value.length == 0 || value == "-") return value;
    if (!isNumber(value)) return value.substring(0, value.length - 1);
    
console.log("minMaxDefrost called ")
    if (parseFloat(value) < min){
        return min;
        }
    else if (parseFloat(value) > max){
        return max;
        }
    else {
        return Math.floor(value);
    }
}


minMaxDefrost(4, 0, 12);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda