These are chat archives for MontCode/GeneralChat

1st
Feb 2017
Matt Richards
@mattjrichards
Feb 01 2017 00:00
Anyone available to help me on FCC lesson #205?
David Castner
@davidjcastner
Feb 01 2017 00:01
yes, can you tell me the name of the lesson or send me a link
not sure where you see the numbers
Matt Richards
@mattjrichards
Feb 01 2017 00:02
It's Using Objects for Lookups.
The lesson number is next to my profile pic in the upper right corner in the header
...or nav bar.
David Castner
@davidjcastner
Feb 01 2017 00:04
one second
Matt Richards
@mattjrichards
Feb 01 2017 00:04
I tried to copy and paste the link, but it was extra extra long.
Not a short link.
This is how I changed it from a switch to a lookup table. I'm stuck on the actual looking up part though.
// Setup
function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
  var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  };


  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");
They want you to use it to lookup val and assign the associated string to the result variable.
David Castner
@davidjcastner
Feb 01 2017 00:09
ah okay
was looking for a good link to help out, but I'll start by asking, do you know the data type of the variable lookup is?
Matt Richards
@mattjrichards
Feb 01 2017 00:15
Data type....String?
Don't I have to make val = lookup somehow?
and then assign it to result?
David Castner
@davidjcastner
Feb 01 2017 00:20

Lookup is considered to be an Object. You can initialize an object like so:

var myNewObject = {};

An object can have values attached to it in a key-value relationship, meaning that for each key there is an associated value. Each key is a string, and each value can be any data type (a string, an integer, another object, or even a function, and more...). In this example: lookup is the object, "alpha"is a key of the object, and "Adams" is the value associated with the key "alpha".

var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
};
make sense so far?
Matt Richards
@mattjrichards
Feb 01 2017 00:22
Yes
David Castner
@davidjcastner
Feb 01 2017 00:23
One way to access the value of a key is with the following syntax:
var value = lookup["alpha"]; // value is now the string "Adams"
Matt Richards
@mattjrichards
Feb 01 2017 00:24
ok
So, I can put that line after my lookup table? That is how you lookup val, correct?
var val = lookup[];
David Castner
@davidjcastner
Feb 01 2017 00:27

Another way to access the value of key is with the following syntax:

var value = lookup.alpha; // value is now the string "Adams"

However, the downside to using this syntax is that the key must be made of a string without certain symbol (such as spaces and periods). Another downside with this is syntax is that you cannot access the value of a key whose key is stored in a variable. There are pros to this syntaxt that I will not get into at this time. But if you need to access the value inside a object when you have the key stored as as variable, you would do the following:

var myKey = "alpha";
var myValue = lookup[myKey];
If you need to read more into objects, this is a good starting pointing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Matt Richards
@mattjrichards
Feb 01 2017 00:29
ok
David Castner
@davidjcastner
Feb 01 2017 00:31
I believe I provided you with enough information to solve the problem, you did a good job with storing the values in a object rather than the switch statement. I try to give out the answers directly but var val = lookup[]; is close. Try using the method above, and let me know if you need more help/explanation.
Matt Richards
@mattjrichards
Feb 01 2017 00:32
Ok, cool. I'm working on it. @davidjcastner thank you.
MontBot
@MontBot
Feb 01 2017 00:32
:cookie: 360 | @davidjcastner |http://www.freecodecamp.com/davidjcastner
mattjrichards sends cookie points to @davidjcastner :sparkles: :thumbsup: :sparkles:
Matt Richards
@mattjrichards
Feb 01 2017 01:26
@davidjcastner I got it.
// Setup
function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
  var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  };
 result = lookup[val]; 



  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");
David Castner
@davidjcastner
Feb 01 2017 02:30
@mattjrichards nice job!
@mattjrichards I should also mention that the keys are also called properties, it depends on the situation and is only a naming thing (has no effct). So if you see the word properties, think keys. And vise versa depending on which you prefer
Matt Richards
@mattjrichards
Feb 01 2017 03:03
@davidjcastner I wish I had a diagram that shows visually what each part of that code does and how it executes on a computer.
Lily Romano
@Lilyheart
Feb 01 2017 03:36
I missed all sorts of chat today :O Sorry friends.
@mattjrichards That number in the corner is a point value. It can change with things other than just doing challenges. Like whn you send someone a tha ks here on the chat, @MontBot’s cookie points given out increase that number. Challenge name is easier to track down.
Matt Richards
@mattjrichards
Feb 01 2017 03:47
hi @Lilyheart
@Lilyheart ahh, wasn't aware of that. I just noticed that seems to increase by one every lesson.
Lily Romano
@Lilyheart
Feb 01 2017 03:50
It does! But Montie can help :p. Thanks @mattjrichards
MontBot
@MontBot
Feb 01 2017 03:50
lilyheart sends cookie points to @mattjrichards :sparkles: :thumbsup: :sparkles:
:cookie: 207 | @mattjrichards |http://www.freecodecamp.com/mattjrichards
Lilyheart @Lilyheart disappears as quickly as she appeared for it is now time for slumber because class is bright and early.
Matt Richards
@mattjrichards
Feb 01 2017 03:56
@Lilyheart lol...thank you!
Scott
@TScottM12
Feb 01 2017 15:34
Hello all, is anyone available to help me out with using PHP in JavaScript? I've built a personality quiz for a friend's website. It's built in JS and loads a new question each time a question is answered (without refreshing or leaving the page), which upon completion chooses a result from an array of 32 personalities. The quiz works just fine, but she wants to be able to edit the quiz result descriptions herself in WordPress. What I've done is make her site into a wordpress theme and then created a post for each personality description, so she can easily edit them. My problem is I haven't been able to figure out how to load these posts into the quiz app itself, since I need JS to look at a database and pull a single post based on the user's quiz results (which I know it can't do directly). I haven't been able to figure out a working solution for this. I have the posts stored in a PHP array (in a quiz.php file), but transfering the data into a JS array in my main.js file is my issue. Any ideas on how to do this?
Lily Romano
@Lilyheart
Feb 01 2017 15:35
I am not that familiar with PHP. Maybe @dcpking ?
I am familar enough to go poking to try to figure things out, but not to solve issues without spending the time to go poking xD
Scott
@TScottM12
Feb 01 2017 15:48

I'm kind of in the same boat. My poking around told me to use

var myVariable = <?php echo json_encode($myVariable); ?>;

but it's giving me a syntax error ("unexpected <"), I'm guessing because I'm trying to use it in a .js file. I think I can just use it in a script tag in my .php file but when I do so the .js doesn't find the variable, which puzzles me. I'm wondering if it has something to do with the way WordPress includes its main.js.

Scott
@TScottM12
Feb 01 2017 17:19

I've noticed if I include the above line of code in my index.php file and check for myVariable in the console, it gives me the content I'm looking for (the PHP data successfully transfers to the JS array). But if I include it in the quiz.php file, myVariable is undefined when the quiz is loaded.

On the homepage (index.php), I have a link:

<input type="button" class="take-quiz" onclick="location.href='/ThemeName/quiz/';" value="TAKE QUIZ" />

When clicked, this loads the quiz. The quiz itself works fine but myVariable is no longer recognized, even if I define it in quiz.php. I'm still learning about WordPress and I think the way it uses templates may be causing my issue. Quiz.php is a custom template file I made, which loads the WordPress PAGE "quiz." So browsing straight to quiz.php in the browser, in other words, would give a 404 error (the correct url is siteurl/quiz/, not siteurl/quiz.php). However, myVariable remains undefined whether I put the json code in quiz.php or directly into the WordPress page that it loads.