Skip to content

[V3] Implement new Concept Exercise: type-conversion #947

Closed
@SleeplessByte

Description

@SleeplessByte

This issue describes how to implement the type-conversion concept exercise for the javascript track.

Getting started

Please please please read the docs before starting. Posting PRs without reading these docs will be a lot more frustrating for you during the review cycle, and exhaust Exercism's maintainers' time. So, before diving into the implementation, please read up on the following documents:

Please also watch the following video:

See the documentation above (general documentation), as well as How to implement a Concept Exercise in JavaScript.

Goal

The goal of this exercise is to teach the student how they can "cast" between types.

JavaScript specificness

ℹ Many languages have a way to cast a value into a (different) type. JavaScript does not have such constructs. However, the global objects for truthy primitives (String, Number, Boolean) also exist as function (String(val), Number(val), Boolean(val)) which convert the input to the primitive type (non-boxed).

Learning objectives

  • Using (some) prototype functions:
    • * ➡ string: .toString()
    • * ➡ Array<*>: [Symbol.iterator] (destructuring works with any iterable)
    • Array<*> ➡ string: .join
    • Date ➡ number: .getTime
    • Date ➡ string: .toISO8601
    • Object ➡ string: JSON.stringify
    • Array ➡ string: JSON.stringify
    • string ➡ Object : JSON.parse
    • string ➡ Array: JSON.parse
  • Using global functions:
    • * ➡ boolean: Boolean(value)
    • * ➡ number: Number(value)
    • * ➡ string: String(value)
    • ...* ➡ Array: Array(...values)
  • Using operators:
    • * ➡ boolean: !value and !!value
  • Using constructors:
    • number ➡ Date: new Date(unix)
    • string ➡ Date: new Date(iso8601)

If this list can not be turned into a single exercise, you can remove the Date related ones, and put that into a dates concept exercise. Also remove the JSON related ones, and put that into a json concept exercise!

Out of scope

  • number#toString(radix) (probably need a different exercise about non-base-10 numbers)
  • new Function(value) (we're not teaching eval here, and does MORE than type conversion)
  • Loose type conversion that also does invalidation:
    • parseInt (probably want an input validation exercise, does MORE than type conversion)
    • parseFloat (probably want an input validation exercise, does MORE than type conversion)
  • Unary operators that basically do an implicit type cast (such as == does)
    • +string (not idiomatic)
    • ~string (not idiomatic)
  • Binary operators that do an implicit type cast (actually ==)
    • string ^ string (not idiomatic)
    • string * string (not idiomatic)

Concepts

  • type-conversion

Prerequisites

  • basics
  • concepts about the types
  • ... (do you think something is missing?)

Help

You can choose to do this solo-style, or collaborate with multiple people on this. My suggestion would be is to

  1. first accept this issue by saying "I'd like to work on this" (you will not get a response from me. Just go with it) and optionally request that someone works with you (and wait for a second person to accept your request).
  2. Use this issue to write down what you need (collect reference material) and discuss, if necessary, what to write and what not to write.
  3. Draft a PR and request the feedback from at least one other JavaScript member (I recommend just pinging the channel on slack).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions