exercism / exercism/javascript-analyzer

Add analyzer for rna-transcription

Open
#53 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
16
Forks
21
Avg merge
6h 53m
Merged PRs (30d)
2

Description

This issue is for discussion and assignment for the [`rna-transcription`][exercise] core exercise in the `javascript` track.

🔗 [implementation][exercise] | [mentor-notes][mentor-notes] | [problem-specification][problem-specification]

----

[mentor-notes]: https://github.com/exercism/website-copy/tree/master/tracks/javascript/exercises/rna-transcription/mentoring.md
[exercise]: https://github.com/exercism/javascript/tree/master/exercises/rna-transcription
[problem-specification]: https://github.com/exercism/problem-specifications/tree/master/exercises/rna-transcription

This exercise focuses on
- [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) iteration ([`String#replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), [`String#split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split))
- [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)/`Map`

## Optimal solution

```javascript
const TRANSCRIPTION = {
C: 'G',
G: 'C',
A: 'U',
T: 'A',
};

export function toRna(sequence) {
return sequence
.split('')
.map(nucleotide => TRANSCRIPTION[nucleotide])
.join('')
}
```

Variations include `Set` or `Map` with `#get`, which are valid!

### Variations (approvable without comment)

String destructuring can also be used:
```javascript
return [...sequence].map(/**/).join('')
```

[String#replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) can also be used:
```javascript
return sequence.replace(/./g, (nucleotide) => /* */)
```

Variations include replacing only the "known" nucleotides:
```javascript
sequence.replace(/[CGAT]g/, (nucleotide) => /* */)
```

### Helper function variation

Instead of an anonymous function, a helper function may be used:

```javascript
function transcribe(nucleotide) {
return TRANSCRIPTION[nucleotide]
}
```

This also allows for a version without a _mapping_ object:

```javascript
function transcribe(nucleotide) {
switch(nucleotide) {
case 'C': { return 'G' }
case 'G': { return 'C' }
case 'A': { return 'U' }
case 'T': { return 'A' }
}
}
```

## SHOULD comment and disapprove

Cases we _SHOULD_ comment on with this analyzer:
- Use an [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) to keep track of the mapping instead of conditionals.
- Use iteration via [`String#split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) or [`String#replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) instead of using `for`/`forEach` with [`Array#push`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)
- Discourage [`Array#reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) for this particular solution, because it creates a lot of intermediary strings (more than the `split` approach), except if the rest of the solution is correct (then you can mention it but approve). Using `reduce` requires more interpretation by the reader to follow, change and maintain.
- Discourage [`String#substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) with foreach iteration, because character iteration via `split('')` is more idiomatic and maintainable than `substring` with 1. Using `split('')` requires less interpretation by the reader to follow, change and maintain.

## Approach

The suggested approach is to:
- detect which _type_ of solution it is (switch, map, or for/forEach)
- make sure the *optimal solution* is correctly handled. You can use the `batch` runner to generate output for all fixtures.
- go from there by adding paths to disapprove.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.