Code Lesson

Markdown

Code

Code blocks are written using triple backticks:

    ```language
    code goes here
    ```
    {: .section-tag}

These sections can be styled according to what they’re supposed to present.

console.log("We use the {: .source} tag to show code that people can write.")
> "We use the {: .output} tag to show expected output."
> "We use the {: .warning} tag to show a warning message."
> "We use the {: .error} tag to show errors."

Advanced Code Tags

You can nest code tags within other tags:

> ## Question
> Currently we have a line of code that produces an error:
> ```javascript
> const hello = ["hello", "world"];
> console.log(hello.combine(', '))
> ```
> {: .source}
> ```
> > Uncaught TypeError: hello.combine is not a function
> ```
> {: .error}
> How can we fix this to write `hello, world` to the console?
>
> > ## Solution
> > We need to use the appropriate method. The `Array` prototype [has a `join` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) that does what we need:
> > ```javascript
> > const hello = ["hello", "world"];
> > console.log(hello.join(', '))
> > ```
> > {: .source}
> >
> > ```
> > > "hello, world"
> > ```
> > {: .output}
> {: .solution}
{: .challenge}

Question

Currently we have a line of code that produces an error:

const hello = ["hello", "world"];
console.log(hello.combine(', '))
> Uncaught TypeError: hello.combine is not a function

How can we fix this to write hello, world to the console?

Solution

We need to use the appropriate method. The Array prototype has a join method that does what we need:

const hello = ["hello", "world"];
console.log(hello.join(', '))
> "hello, world"