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
2 Answers
Reset to default 5You 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