Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
¿Alguien puede decirme por qué recibo un error cuando intento llamar a esta función?
<!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>
  • Utilizo esta función en una función de teclado html y funciona bien, pero cuando trato de llamarla sola al comienzo del código, aparece el mensaje de error Uncaught TypeError: value.substring is not a function
  • tampoco cuando llamo a esta función desde el teclado, uso "this.value = minMaxDefrost (this.value, -80, 150, 0)
  • Supongo que el error tiene que ver con este valor, pero no sé cómo solucionarlo.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

isNumber(4) se evalúa como false , por lo que está intentando llamar a un método de substring en 4 pero no existe. Use typeof value === 'number' en su lugar para probar si algo es un número. O mejor aún, use typeof value === 'string' antes de tratarlo como si fuera definitivamente una cadena.

about 4 years ago · Juan Pablo Isaza Report

0

isNumber no hace exactamente su trabajo como sugiere Mark Hanna... un mejor isNumber sería algo como

 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 }

Aquí está devuelto en el código que nos diste

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!