-1

http://eloquentjavascript.net/04_data.html

Please. Scroll down to the headline "The lycanthrope’s log" . It introduces the phi correlation which took me a while to understand, If you scroll down a little bit further, You will find the headline Computing correlation.

var journal = [];

function addEntry(events, didITurnIntoASquirrel) {
  journal.push({
    events: events,
    squirrel: didITurnIntoASquirrel
  });
}
    function phi(table) {
  return (table[3] * table[0] - table[2] * table[1]) /
    Math.sqrt((table[2] + table[3]) *
              (table[0] + table[1]) *
              (table[1] + table[3]) *
              (table[0] + table[2]));
}

Top^^^^The code that is also part of the full "program". I've put my "QUESTIONS" in comments in the code below. Please help! If anyone can also answer the comments in my question i will be VERY GRATEFUL!!!!!

function hasEvent(event, entry) {
  return entry.events.indexOf(event) != -1;
/*What does "events" do?Why -1?How does indexOf work?"*/
}

function tableFor(event, journal) {
  var table = [0, 0, 0, 0];
 /*How does the program figure out each of the values for this table?*/
/*and how does it know when one value ends and the time to start*/
/*calculating the next value*/

    for (var i = 0; i < journal.length; i++) {
    var entry = journal[i], index = 0;/*???? why 0*/
    if (hasEvent(event, entry)) index += 1;
    /*I dont understand how this function works*/
    if (entry.squirrel) index += 2;/*why +2?*/
    table[index] += 1;/*what exactly is index???*/
  }
  return table;
}

I'm really new to teaching myself programming. Eloquent javascript is supposed to be for beginners and i'm already struggling :\

Midnight
  • 9
  • 2
  • You need to ask specific, focused questions, and it shouldn't be necessary to leave your question to another site. – djechlin Dec 24 '15 at 04:36
  • Please dont downvote man. Sorry, I'm new to programming n also stackoverflow..What do you mean specific focused questions? I have multiple questions on the same topic :( – Midnight Dec 24 '15 at 04:41
  • http://stackoverflow.com/help/how-to-ask – djechlin Dec 24 '15 at 04:43
  • @Midnight You need to google your confusions one by one and take proper time understand the concepts behind all these. You can get some idea how indexOf works from here : http://stackoverflow.com/questions/26313644/how-javascript-indexof-works-differently-with-string-and-array – Jitendra Khatri Dec 24 '15 at 05:06
  • Have you tried reading the web page you referenced? Right after the above code there's a paragraph that explains what `indexOf` does. – Barmar Dec 24 '15 at 05:20
  • It say: **So if the call to `indexOf` doesn’t return -1, then we know the event was found in the entry.** – Barmar Dec 24 '15 at 05:20

1 Answers1

0

indexOf is a method available on Strings and Arrays in Javascript. You can use it to find something in either one of the objects. It returns -1 if it doesn't find the thing you're looking for in the String or Array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

return entry.events.indexOf(event) != -1; Is trying to figure out if the event you're asking about, exists within the array of events for a given entry by checking the return value of indexOf. This is a common Javascript idiom.

var entry = journal[i], index = 0;/*???? why 0*/ this assigns a reference to value from journal[i] into the entry variable. It also assigns the integer value 0 the index variable.

This multiple variables being defined on a single line. How to define multiple variables on a single line?

This is an if statement that only executes the code directly following it. This is terrible, but valid, syntax. You should never do this in real life. Always use braces. if (hasEvent(event, entry)) index += 1;

Are curly braces necessary in one-line statements in JavaScript?

I've transformed the code you posted into something more readable by getting rid of all the syntactic junk the author put in there. My only guess with table is that it's trying to record all the different possible outcomes from the ifs here. You get 0 if there's no event, and there's no squirrel. You get 1 if there's an event but no squirrel. You get 2 if there's no event, but there is a squirrel. And you get 3 if there is an event, and a squirrel. You then increment one of four different positions in the table variable based on what transpired.

Since you're iterating over a journal with i, my assumption is that you're trying to keep track of what's happening to you over a long period of time. So if you had, 1 journal entry that added up to 10 and 5 journal entries that added up to 1, and 3 journal entries that added up to 2, then you'd have a table variable with [10, 5, 2, 0] in it.

You can basically think of index as keeping track of all possible states. If there were a third possibility, such as "there is a shark", and it could be independently true or false like the other ones, the index could have a total possible value of 7 (1 + 2 + 4) because the shark one would have to add 4 to index to not step on the toes of the previous two. This is related to the sum of the powers of two SUM (2^n) where n is the number independently possible different things that could happen to you.

for (var i = 0; i < journal.length; i++) {
    var entry = journal[i];
    var index = 0;
    if (hasEvent(event, entry)) {
        index += 1;
    }

    if (entry.squirrel) {
        index += 2;
    }
    table[index] += 1;
}

Generally if you're going to get into programming you need to learn to Google this stuff for yourself. It can be difficult at first, but it is an invaluable skill to learn. I'd rather hire a novice developer that could Google his way out of a problem then an intermediate developer who couldn't reliably find answers on Google.

Community
  • 1
  • 1
Jazzepi
  • 5,259
  • 11
  • 55
  • 81
  • Man, thank you so much for replying. I just saw your answer now.I will read it in full. Just wanted to make a quick post saying a BIG thank you for taking the time out to help a newbie out! – Midnight Dec 24 '15 at 05:48
  • Man you write much more eloquently than the author! Thank you so much for taking the time to post that answer. Your answer made a lot of sense and was easy to understand. I usually get answers from google, but was stuck at this problem for HOURS- like 8+ haha how stupid is that :\ i used to wake up only thinking about this. Now i can resume learning with enthusiasm i thought i had lost!! Good day to you my friend. – Midnight Dec 24 '15 at 11:16
  • I think in another life I would be a teacher. Glad you find it instructive. I know how it feels to get stuck. Good luck with the rest of your studies. – Jazzepi Dec 25 '15 at 00:16