class Test extends React.Component{
state={name: "John", numTimes: 2};
render() {
let output = ""
for (let i = 1; i <= this.state.numTimes; i++) {
let evenOdd = i % 2
if (evenOdd === 0) {
output += i + ". Hello " + this.state.name + "!"
} else {
output += i + ". Hello " + this.state.name
}
}
return <p>{output}</p>
}
}
ReactDOM.render(<Test /> , document.getElementById("react"));
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
I am setting up an application where it will take a users name by input and how many times they want their name to display.
I have tried styling it, adding my own break line to the output either using "\n" and breaklines.
render() {
let output = ""
for (let i = 1; i <= this.state.numTimes; i++) {
let evenOdd = i % 2
if (evenOdd === 0) {
output += i + ". Hello " + this.state.name + "!"
} else {
output += i + ". Hello " + this.state.name
}
}
return <p>{output}</p>
}
My for loop it will print out ! on even and nothing on odd so if they do 2 for the number the desired outcome would be
1. Hello John
2. Hello John!
etc...
I don't get any errors just an incorrect output of...
- Hello John 2. Hello John!
'` if you want a newline - `\n` is not interpreted by your browser unless you use `white-space:pre` on the container – Pete Jun 07 '19 at 14:27