• Empleos
  • Sobre nosotros
  • Empleos
    • Inicio
    • Empleos
    • Cursos y retos
  • Empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

236
Vistas
remove the sign at the beginning of the number - JS

I am getting a calculation result minus the resulting number. When I want to remove the minus sign in front of this number, I get a ".. is not function" warning. What is the reason of this?

function cms(miliseconds, format) {
    let days, hours, minutes, seconds, total_hours, total_minutes, total_seconds;
    total_seconds = parseInt(Math.floor(miliseconds / 1000));
    total_minutes = parseInt(Math.floor(total_seconds / 60));
    total_hours = parseInt(Math.floor(total_minutes / 60));
    days = parseInt(Math.floor(total_hours / 24));
    seconds = parseInt(total_seconds % 60);
    minutes = parseInt(total_minutes % 60);
    hours = parseInt(total_hours % 24);
    switch (format) {
        case 's':
            return total_seconds;
        case 'm':
            return total_minutes;
        case 'h':
            return total_hours;
        case 'd':
            return days;
        default:
            return {d: days, h: hours, m: minutes, s: seconds};
    }
}

const cd3F = cms(...); // Result: -3512
cd3F.slice(1));

Uncaught TypeError: cd3F.slice is not a function is not a function

over 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your cms function is either going to return a Number or an Object, and neither of them have a slice method.

A better function to use would be Math.abs(), which will convert any negative number into a positive one (and leave any positive number alone) but be aware that it will return NaN if you pass it an object.

over 3 years ago · Juan Pablo Isaza Denunciar

0

The .slice method is for strings and arrays, not for numbers.

function cms(miliseconds, format) {
    let days, hours, minutes, seconds, total_hours, total_minutes, total_seconds;
    total_seconds = parseInt(Math.floor(miliseconds / 1000));
    total_minutes = parseInt(Math.floor(total_seconds / 60));
    total_hours = parseInt(Math.floor(total_minutes / 60));
    days = parseInt(Math.floor(total_hours / 24));
    seconds = parseInt(total_seconds % 60);
    minutes = parseInt(total_minutes % 60);
    hours = parseInt(total_hours % 24);
    switch (format) {
        case 's':
            return total_seconds;
        case 'm':
            return total_minutes;
        case 'h':
            return total_hours;
        case 'd':
            return days;
        default:
            return {d: days, h: hours, m: minutes, s: seconds};
    }
}

const cd3F = cms(...); // Result: -3512
const abs_cd3F = Math.abs(cd3F);

You should use Math.abs. Only if it were a string, you could use .slice(1).

If you only want positive numbers then you might as well put Math.abs inside your function.

function cms(milliseconds, format) {
    milliseconds = Math.abs(milliseconds);
    let days, hours, minutes, seconds, total_hours, total_minutes, total_seconds;
    total_seconds = parseInt(Math.floor(milliseconds / 1000));
    total_minutes = parseInt(Math.floor(total_seconds / 60));
    total_hours = parseInt(Math.floor(total_minutes / 60));
    days = parseInt(Math.floor(total_hours / 24));
    seconds = parseInt(total_seconds % 60);
    minutes = parseInt(total_minutes % 60);
    hours = parseInt(total_hours % 24);
    switch (format) {
        case 's':
            return total_seconds;
        case 'm':
            return total_minutes;
        case 'h':
            return total_hours;
        case 'd':
            return days;
        default:
            return {d: days, h: hours, m: minutes, s: seconds};
    }
}

const cd3F = cms(-3512000, "s"); // Result: 3512
over 3 years ago · Juan Pablo Isaza Denunciar

0

Why so complicated? n * -1

const cd3F = cms(...); // Result: -3512
cd3F = cd3F < 0 ? cd3F * -1 : cd3F;
console.log(cd3F); // 3512
over 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda