The process about Pass an Array as Props

Tell us what’s happening:
Why is it that a comma have to be followed by a space in {props.tasks.join(", ")}?And what will it be after {props.tasks.join(", ")} in my code? In other words,what happened in the process?

Your code so far


const List= (props) => {
  { /* change code below this line */ }
    return <p>{props.tasks.join(", ")}</p>;
  { /* change code above this line */ }
};

class ToDo extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <h1>To Do Lists</h1>
        <h2>Today</h2>
        { /* change code below this line */ }
        <List tasks={["green","blue","red"]} />
        <h2>Tomorrow</h2>
        <List tasks={["green","blue","red"]} />
        { /* change code above this line */ }
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/pass-an-array-as-props/

The , is the speparator that goes between the array items when you join them into a string. If you do comma, space there will be a comma and a space between them, just a comma will be just a comma

.join() method works like a glue. It glues together elements of an array and uses the argument you pass to it as concatenating string.

So in your case, it uses a coma and blank space to glue elements of an array together into a string.

for example

var array = ['apple', 'strawberry', 'orange'];
var arrayString = array.join(', ');
consolge.log(arrayString) // Output will be: apple, strawberry, orange

More information is available at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Thank U,maybe I have not explain it clearly.I wants to know what does {props.tasks.join(", ")} work for ,and why is it that my code won’t pass when I remove the space after the comma.

props.tasks is an array, you are rendering the array out as a comma-seperated string, so you use join to turn the array into a string. It doesn’t pass the test because the test expects you to have spaces after each comma

Get it.Thank u very much!

1 Like