ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

issue with making a array a function #181

Closed ArmaniH closed 9 years ago

ArmaniH commented 9 years ago

I tried adding an array to a function. I expected it to populate an element on a click event . Nothing happens instead. I tried using hard coded text to do the same thing and it worked fine, but I need a value from an array to do the same thing. Ultimaely being that a random value from the array will be called with each click event. I'm workign on the trivia project.

var triviaQuestions = ['How tall am I?', 'How old am I?'];
    console.log(triviaQuestions)

//Clicking start displays first question and 4 potential answers

  $('.start').click(function(e) {
    console.log(triviaQuestions)
    e.preventDefault();
    $('p').replaceWith(function(triviaQuestions) {
      return $(this).append(['<p>How tall am I?</p>']); //<-this line I added to text if a hardcoded string would appear and it did. Without it the space is just blank
    });
    $('p').show('slow');
    $('h2').show('slow');
    })
RobertAKARobin commented 9 years ago

One problem is this:

$(this).append(['<p>How tall am I?</p>']);

I'm not sure why that's in array brackets?

RobertAKARobin commented 9 years ago

I think you're making things a little over-complicated. This portion:

    $('p').replaceWith(function(triviaQuestions) {
      return $(this).append(['<p>How tall am I?</p>']); //<-this line I added to text if a hardcoded string would appear and it did. Without it the space is just blank
    });

...can be rewritten like this:

    $('p').replaceWith('<p>How tall am I?</p>');
ArmaniH commented 9 years ago

That and some syntax altering took care of it! Thank you!