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

312
Views
Function does not work when entering number javascript

I have a function that reverses a string. I would like the function to return "Null" when a number is entered however when i input a number my function stops working. Is there something i am missing?

function reverseWords(words) {
    let eachLetter = words.split("");

    let reverseLetter = eachLetter.reverse();

    let combineLetter = reverseLetter.join("");

    if (typeof words !== "string") {
        return null;
    } else {
        return combineLetter;
    }
}

console.log(reverseWords(89));

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

the operations you try to perform before the if are not possible with numbers.

Put validation as the first thing your function does and everything will work.

about 4 years ago · Juan Pablo Isaza Report

0

function reverseWords(words) {

if (typeof words !== "string") {
    return null;
} else {
        
   //Split the string 
  let eachLetter = words.split("");

  let reverseLetter = eachLetter.reverse();

  let combineLetter = reverseLetter.join("");


    return combineLetter;
}

}

console.log(reverseWords('89'));

You cannot apply 'split' method on numbers. First, check for the numeric value and perform the split function only when it is a string.

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!