0

This code is returning 268, but it's returning 268 as a number when I'd like for it to return 134134 as a string:

function numbersToStrings(input1, input2) {

    let newString = " ";

    if (input1 === Number) {
        input1 = input1.toString();
    }
    if (input2 === Number) {
        input2 = input2.toString();
    }

    return input1 + input2;
}

console.log(numbersToStrings(134, 134));

I've checked if the input is a number with if (input1 === Number) - is that not a valid way to check if something is a number in JavaScript?

I'm working on a problem that does not allow the use of .concat() or .join(), so please tell me why my number check isn't working, or why .toString() isn't working.

Thanks.

9
  • 2
    typeof input1 === "number" Commented Apr 6, 2019 at 21:37
  • 1
    "is that not a valid way to check if something is a number in JavaScript?" No. You are checking whether the value is equal to the Number function. And there is only one value for which this is true, Number itself. Commented Apr 6, 2019 at 21:38
  • 1
    Number is the global object window.Number, which definitely isn't what you want Commented Apr 6, 2019 at 21:38
  • Do you want 134 + 134 to be 268 as a string or 134134? Commented Apr 6, 2019 at 21:40
  • 1
    If you're comfortable with ES6, I'd suggest just using return `${input1}${input2}` Commented Apr 6, 2019 at 21:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.