2

i am trying to get a random item from a list. so far i managed to put my list in an array and i'm trying to get the item i need with a function but where i need it , it's undefined! i don't know where i am wrong!!!

i wanna see the result in this console log

 handleChange(event) {
    this.setState({value: event.target.value});
    var NewDictionary = this.Dictionary;
    console.log(Rand(NewDictionary));
  }

the whole code is here:

class Application extends React.Component{
  constructor(props) {
   super(props);
   this.state = {
     value:'option2',
     button:'submit',
   };
   this.Dictionary = {
     Salam:"Hi",
     khodafez:"Bye",
     are: "Yes",
     na: "No",
     shab: "Night",
     rooz: "Day",
   }
   this.handleChange = this.handleChange.bind(this);
   this.handleSubmit = this.handleSubmit.bind(this);
 }
 handleChange(event) {
    this.setState({value: event.target.value});
    var NewDictionary = this.Dictionary;
    console.log(Rand(NewDictionary));
  }
  handleSubmit(event) {
    this.setState({button: this.state.value});
    event.preventDefault();
  }

  render(){
    return(

  <form onSubmit={this.handleSubmit}>
  <div> {this.Dictionary.Salam}  </div>
  <div className="radio">
    <label>
      <input type="radio" value={this.Dictionary.Salam}
        checked={this.state.value === this.Dictionary.Salam}
        onChange={this.handleChange} />
        {this.Dictionary.Salam}
    </label>
  </div>
  <div className="radio">
    <label>
      <input type="radio" value="option2"
        checked={this.state.value === 'option2'}
        onChange={this.handleChange} />
      Option 2
    </label>
  </div>
  <div className="radio">
    <label>
      <input type="radio" value="option3"
        checked={this.state.value === 'option3'}
        onChange={this.handleChange} />
      Option 3
    </label>
  </div>
  <div className="radio">
    <label>
      <input type="radio" value="option4"
        checked={this.state.value === 'option4'}
        onChange={this.handleChange} />
        Option 4
    </label>
  </div>
  <button onClick={this.handleSubmit} className="btn btn-default" type="submit">{this.state.button}</button>
</form>

     )
   }
}
function Rand(NewDictionary){
  let i = NewDictionary.length - 1;
  const j = Math.floor(Math.random() * i);
  return NewDictionary[j];
}
ReactDOM.render(
  <Application />,
  document.getElementById('root')
);
2
  • 4
    this.Dictionary is an object, not an array. Commented Sep 5, 2017 at 19:32
  • 3
    Your dictionary is an Object, not an Array. Use Object.keys(NewDictionary) to get an array of its keys. Commented Sep 5, 2017 at 19:32

1 Answer 1

1

It's because NewDictionary is an object and not an array, so trying to get an item by an index won't work. Instead, use Object.keys(NewDictionary) to create an array of keys, and then use the random index to get a random key.

function Rand(NewDictionary){
  const keys = Object.keys(NewDictionary);
  let i = keys.length - 1;
  const j = Math.floor(Math.random() * i);
  return NewDictionary[keys[j]];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.