Home > Blockchain >  remove the sign at the beginning of the number - JS
remove the sign at the beginning of the number - JS

Time:04-29

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

Why so complicated? n * -1

const cd3F = cms(...); // Result: -3512
cd3F = cd3F < 0 ? cd3F * -1 : cd3F;
console.log(cd3F); // 3512

CodePudding user response:

Numbers nor Objects have the slice method so for strings you'll first have to make the following changes

cd3F.slice(1));

to

cd3F.toString().slice(1))

This doesn't solve your problem with all use cases though because you have the potential to return an object.

I'd use Math.abs() because it will return either NaN (if it is an object) or a positive integer if it is indeed a Number

  • Related