Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

308
Visualizações
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

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório

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
about 4 years ago · Juan Pablo Isaza Relatório

0

Why so complicated? n * -1

const cd3F = cms(...); // Result: -3512
cd3F = cd3F < 0 ? cd3F * -1 : cd3F;
console.log(cd3F); // 3512
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda