javascript - Select from tuple with Typescript - Stack Overflow

I'm working through a course, and the current challenge is to "Write a function that takes an

I'm working through a course, and the current challenge is to "Write a function that takes an array of names and ages, then returns only the names."

So far my approach has looked like this:

function names(arr: [string, number]) {
  let result: [] = [];
  arr.forEach((element) => {
    if (typeof(element) === "string") {
      result.push(element);
    }
  })
  return result;
}

I'm trying to get tests to pass that assert that I am actually getting just the strings returned from my function. I'm at a loss, the current error I'm getting from my attempt at a solution is

names([['Amir', 34], ['Betty', 17]])

Expected: ['Amir', 'Betty'] but got: type error: Argument of type 'string' is not assignable to parameter of type 'never'.

Any help is greatly appreciated!

I'm working through a course, and the current challenge is to "Write a function that takes an array of names and ages, then returns only the names."

So far my approach has looked like this:

function names(arr: [string, number]) {
  let result: [] = [];
  arr.forEach((element) => {
    if (typeof(element) === "string") {
      result.push(element);
    }
  })
  return result;
}

I'm trying to get tests to pass that assert that I am actually getting just the strings returned from my function. I'm at a loss, the current error I'm getting from my attempt at a solution is

names([['Amir', 34], ['Betty', 17]])

Expected: ['Amir', 'Betty'] but got: type error: Argument of type 'string' is not assignable to parameter of type 'never'.

Any help is greatly appreciated!

Share Improve this question asked Jan 3, 2020 at 17:00 will-t-harriswill-t-harris 981 silver badge11 bronze badges 4
  • What does your test look like? – Rastalamm Commented Jan 3, 2020 at 17:02
  • Uh, your input is an array of arrays. Not an array of a string and number – Taplar Commented Jan 3, 2020 at 17:04
  • you need to get the first parameter of the element like: element[0] === "string" – Luis Louis Commented Jan 3, 2020 at 17:04
  • What specifically does "an array of names and ages" mean? – jcalz Commented Jan 3, 2020 at 17:08
Add a ment  | 

2 Answers 2

Reset to default 5

You just need to define

let result: [] = [];

as

let result: string[] = [];

Also looks like your parameter is an array of arrays. So you need to define

arr: [string, number]

as

arr: [string, number][]

or equivalent syntax is

arr: Array<[string, number]>

and then change you for loop as well. Something like that.

function names(arr: [string, number][]) {
  let result: string[] = [];
  arr.forEach(([element]) => {
    result.push(element);
  })
  return result;
}

Note that I use Destructuring assignment to get first element.

The solution mentioned above is fine but we can easily write the same code with the ES6 map function if you carefully look at the question they are saying it takes an array of [names and ages] so it means [[name1,age1], [name2,age2] .........]

so the return type should be [string, number][]

This is my approach :

function names(namesAndAges: [string,number][]): string[] {
  return namesAndAges.map(nameAndAge => nameAndAge[0]);
}

Hope it helps it is just using map function and returning only the names from name and age array

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435254a4627579.html

相关推荐

  • javascript - Select from tuple with Typescript - Stack Overflow

    I'm working through a course, and the current challenge is to "Write a function that takes an

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信