Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

function returns void even if i set the return statement to array?

I am working on this function that takes an array of distance meters is the default, and converts is back to feet and miles, the problems is I am getting this error I stated that option must return an array of numbers any idea where is this syntax error.
Type ‘void[]’ is not assignable to type ‘number[]’.

const option = () => {
    let arr = [200, 400, 800, 1000, 1, 2, 3, 4, 5];
    
      return arr.map((x, i) => {
        i <= 3 ? x * 3.281 : x / 1.609;
      });
   }
    
    
  console.log(option());

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are both missing a return and not: when using an arrow function, if your function body is a single statement, you can omit both the {} and the return statement:

const option = () => {
    let arr = [200, 400, 800, 1000, 1, 2, 3, 4, 5];
    
      return arr.map((x, i) => i <= 3 ? x * 3.281 : x / 1.609);
   }
    
    
  console.log(option());

Incidentally, this also lets you do things like:

const materials = [
  'Hydrogen',
  'Helium',
  'Lithium',
  'Beryllium'
];

console.log(materials.map(material => material.length));

Ref

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading