Implement JavaScript utility and API functions
Company: Sap
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given a small JavaScript coding exercise with a local dataset `authors` and access to the public Pokémon API.
Assume `authors` is an array of objects shaped like:
```js
// authors = [
// { firstName: string, lastName: string, nationality: string },
// ...
// ]
```
Implement the following functions (you may use modern JavaScript/TypeScript style, and you may use `async/await` for async functions):
## 1) Basic functions
1. **`strConcat(a, b)`**
- Concatenate two strings and return the result.
2. **`sum(x, y)`**
- Return the sum of two numbers.
## 2) Working with the `authors` array
3. **`authorNames()`**
- Return an array of each author’s full name.
- Example output shape:
```js
["Robert Frost", "Philip Dick", ...]
```
4. **`authorsAreAuthors()`**
- Return a single sentence listing all author full names, followed by `" are authors."`.
- Example format:
- `"Robert Frost, Philip Dick, ... are authors."`
- Use sensible punctuation for lists (commas; optional Oxford comma is acceptable).
5. **`americanAuthors()`**
- Return a sentence listing only authors whose `nationality` is `"American"`, followed by `" are American authors."`.
- Example:
- `"Robert Frost, Emily Dickinson, and Philip Dick are American authors."`
6. **`authorNationality()`**
- Group authors by `nationality` and return a dictionary/object of this shape:
```js
{
French: {
authors: ["Albert Camus", ...],
count: 2
},
American: {
authors: ["Robert Frost", ...],
count: 3
},
...
}
```
- `authors` should be full-name strings.
- `count` should match the number of authors for that nationality.
## 3) JavaScript feature questions
7. **`altSum(x)(y)`**
- Implement a curried function where calling `altSum(x)(y)` returns `x + y`.
8. **`nSum(...nums)`**
- Implement a function that accepts any number of numeric arguments and returns their total.
- Example: `nSum(1, 2, 3, 4) === 10`.
## 4) Pokémon API (network requests)
Use the public API at `https://pokeapi.co/`.
9. **`getPokemon()`**
- Fetch the first 10 Pokémon from the API endpoint:
- `GET https://pokeapi.co/api/v2/pokemon?limit=10`
- Return an array of objects containing only:
```js
[{ name, url }, ...]
```
10. **`displayPokemon()`**
- Fetch the first 10 Pokémon (via `getPokemon()`), then fetch each Pokémon’s detail URL.
- Return an array of objects shaped like:
```js
[
{
name,
height,
weight,
img // from sprites.front_default
},
...
]
```
### Notes
- Keep return types consistent.
- Handle empty lists gracefully.
- For API calls, you may fetch sequentially or concurrently (as long as the output is correct).
Quick Answer: This question evaluates JavaScript fundamentals including string and numeric operations, array transformation and grouping, functional concepts such as currying and variadic arguments, and asynchronous HTTP interactions with a REST API.