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

305
Views
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 answers
Answer question

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 Report

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 Report

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 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!