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

Advertisements

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 :

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

Leave a ReplyCancel reply