These are chat archives for FreeCodeCamp/Help

7th
Jan 2015
Wynda
@Wynda
Jan 07 2015 01:26

I am currently stuck. How do you "Create a new style up in CSS that gives div a height of 200px"? I thought it would be in the body I would put
{

<div style= "height 200px">;
</div>
}
Thanks

Charles Watson
@sircharleswatson
Jan 07 2015 01:27
div {
   height: 200px;
}
Maxim Orlov
@Maximization
Jan 07 2015 01:28
you insert the above code between the <style> and </style> tags
Wynda
@Wynda
Jan 07 2015 01:31
Thank you so much, it worked. = )
GrahamFischer
@GrahamFischer
Jan 07 2015 03:07

I'm wondering if anyone can give me a bit of guidance on some jquery. I'm on section 3 (traversing the DOM) challenge 13. I'm supposed to use traversal to select all tours who have a featured class by getting the parent of featured titles. The html in the example is as follows

<div id="tours-wrapper">
  <h1>Guided Tours</h1>
  <ul id="tours">
    <li class="usa">
      <h2>New York, New York</h2>
      <span class="details">$1,899 for 7 nights</span>
      <ul class="vote"><li><a href="#">↑</a></li><li><a href="#">↓</a></li></ul>
    </li>
    <li class="europe">
      <h2>Paris, France</h2>
      <span class="details">$2,499 for 7 nights</span>
      <ul class="vote"><li><a href="#">↑</a></li><li><a href="#">↓</a></li></ul>
    </li>
    <li class="europe sale">
      <h2 class="featured">Madrid, Spain</h2>
      <span class="details">$1,577 for 5 nights</span>
    </li>
    <li class="asia">
      <h2>Tokyo, Japan</h2>
      <span class="details">$1,999 for 5 nights</span>
      <ul class="vote"><li><a href="#">↑</a></li><li><a href="#">↓</a></li></ul>
    </li>
  </ul>

  <ul class="sorting">
    <li><a href="#">America</a></li>
    <li><a href="#">Europe</a></li>
    <li><a href="#">Asia</a></li>
  </ul>
</div>

I've been stuck for so long I used the hint to find the answer which is apparently this;

$('.featured")

But I'm not sure how to use that.

And apparently can't format in here either...
Russ Eby
@RussEby
Jan 07 2015 03:08
It's the back tick next to the 1 that formats the code.
GrahamFischer
@GrahamFischer
Jan 07 2015 03:08
This message was deleted
Russ Eby
@RussEby
Jan 07 2015 03:09
hello
The first set of back ticks had code on it.
GrahamFischer
@GrahamFischer
Jan 07 2015 03:10
Oh
Thank you. I'll repost the code again.
Charles Watson
@sircharleswatson
Jan 07 2015 03:12
you can edit your previous post
GrahamFischer
@GrahamFischer
Jan 07 2015 03:12
Oh that's right
I've edited the original post
Tomy Lim
@Nocando
Jan 07 2015 03:14
I need help
Anton Antonov
@mainevent
Jan 07 2015 03:15
? @Nocando
Tomy Lim
@Nocando
Jan 07 2015 03:15
var password = "I_love_You";

$('#answer').click(function(){
  $('#answer').html(password);
});
I want to toggle the password
and my original text is forgot your password?
GrahamFischer
@GrahamFischer
Jan 07 2015 03:16

Ok I just figured it out. Apparently the proper code was

$(".featured").parent();

Still shaky on the details but I got it. Thank you anyway!

Russ Eby
@RussEby
Jan 07 2015 03:17
@GrahamFischer Cool, glad you got it.
GrahamFischer
@GrahamFischer
Jan 07 2015 03:24
@RussEby Thank you.
@RussEby and thanks for the formatting tip too
Anton Antonov
@mainevent
Jan 07 2015 03:26
@Nocando This method does not accept any arguments.
@Nocando .html()
Charles Watson
@sircharleswatson
Jan 07 2015 03:27
@mainevent .html() does accept arguments :)
Tomy Lim
@Nocando
Jan 07 2015 03:28
errrr let me link to my codepen and explain what i'm trying to do
dammit.... i forgot to save it x-x
Anton Antonov
@mainevent
Jan 07 2015 03:29
@sircharleswatson Sorry, I've read only first section)
Charles Watson
@sircharleswatson
Jan 07 2015 03:29
no worries :)
I want to click forgot your password?
and toggle it to the password
and I want to click it again to toggle it back to its original state
Charles Watson
@sircharleswatson
Jan 07 2015 03:30
it worked for me
Tomy Lim
@Nocando
Jan 07 2015 03:30
idk how to use the .toggle()
Charles Watson
@sircharleswatson
Jan 07 2015 03:30
but not going back
Tomy Lim
@Nocando
Jan 07 2015 03:31
ya i have no idea how to go back
hmm i was thinking to write like a variable to active the class to show and hide
Russ Eby
@RussEby
Jan 07 2015 03:33
My solution would be to check what was written there and using if statements change it from one to another.
Kind of brute force, not sure how to get toggle to work though.
Charles Watson
@sircharleswatson
Jan 07 2015 03:34
try this:
$('#answer').click(function(){
  if (this.text() == "forgot my password") {
    $('#answer').html(password);
  } else {
    this.text("forgot my password");
  }
});
Tomy Lim
@Nocando
Jan 07 2015 03:35
This is very odd..... It's not clicking
well not letting me click it
Charles Watson
@sircharleswatson
Jan 07 2015 03:36
did you copy and paste what I put?
Tomy Lim
@Nocando
Jan 07 2015 03:36
yup
Charles Watson
@sircharleswatson
Jan 07 2015 03:36
that's why ;)
because I didn't capitalize the words like you did
nevermind, that's not why :P
Tomy Lim
@Nocando
Jan 07 2015 03:37
haha
this is my first time writing code on my own without a tutorial x-x
Charles Watson
@sircharleswatson
Jan 07 2015 03:38
change the this.text() to be $(this).text()
@Nocando did that work ^
Tomy Lim
@Nocando
Jan 07 2015 03:40
nope
Charles Watson
@sircharleswatson
Jan 07 2015 03:40
what? I got it working on mine
Tomy Lim
@Nocando
Jan 07 2015 03:40
i think i must've changed something by accident
Charles Watson
@sircharleswatson
Jan 07 2015 03:40
make sure you put the $() around the second this
Russ Eby
@RussEby
Jan 07 2015 03:40
Here's my pen
Tomy Lim
@Nocando
Jan 07 2015 03:42
I got it to work
whew
thank you guys
this is actually fun.... problem solving with other people
Charles Watson
@sircharleswatson
Jan 07 2015 03:43
ya :)
Tomy Lim
@Nocando
Jan 07 2015 03:44
I will ask if I run into something else :D
Russ Eby
@RussEby
Jan 07 2015 03:44
That was cool. I'm learning more about JS just following links here then most of the tutorials I've been reading.
Tomy Lim
@Nocando
Jan 07 2015 03:45
mhm, it is cool
I didn't use if and else because I haven't gone over the challenge
Bin Ury
@teddy-error
Jan 07 2015 03:54
What is the pair programming application that was used today?
Charles Watson
@sircharleswatson
Jan 07 2015 04:00
I think it was screenhero
Bin Ury
@teddy-error
Jan 07 2015 04:13
ty @sircharleswatson
Ian Yeager
@iyeager2004
Jan 07 2015 04:15
  return str.split("").join("").replace(/ /g,'') == 
    str.split("").reverse().join("").replace(/ /g,'');
Is this completely unreadable, or does it make sense, at least a little bit if you had maybe a comment or the function was called isPalindrome?
Ryan Malm
@Rybar
Jan 07 2015 04:18
not unreadable, but why split and then join?
Russ Eby
@RussEby
Jan 07 2015 04:18
Split the string into an array, reverse the array and then join back to a string
Ben Clist
@BenClist
Jan 07 2015 04:19
but why do it the first time? There's an identical split and join straight after each other
Ryan Malm
@Rybar
Jan 07 2015 04:20
^
I get using it for reversal, but it's not necessary the first time.
Ian Yeager
@iyeager2004
Jan 07 2015 04:20
True that. Jeez, that was dumb. I just copied my reversing code.
  return str.replace(/ /g,'') == str.split("").reverse().join("").replace(/ /g,'');
That works too.
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 15:58
$(document).ready(function(){
  $("button").on("click", function(){
    var tour = $(this).closest(".tour").data("discount");
    var message = $("<span>Call 1-555-jquery-air for a $" + tour + " discount.</span>");
    tour.append(message);
    $(this).remove();
  });
});
Can anyone find the problem?
Maxim Orlov
@Maximization
Jan 07 2015 16:00
the initialization of your message variable should just be a simple string I think
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:01
don't get it
Maxim Orlov
@Maximization
Jan 07 2015 16:02
no I was mistaken, it's ok like this
do you have a link to the lesson?
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:03
yup
^
<div id="tours">
  <h1>Guided Tours</h1>
  <ul>
    <li class="usa tour" data-discount="299">
      <h2>New York, New York</h2>
      <span class="details">$1,899 for 7 nights</span>
      <button class="book">Book Now</button>
    </li>
    <li class="europe tour" data-discount="176">
      <h2>Paris, France</h2>
      <span class="details">$2,299 for 7 nights</span>
      <button class="book">Book Now</button>
    </li>
    <li class="asia tour" data-discount="349">
      <h2>Tokyo, Japan</h2>
      <span class="details">$3,799 for 7 nights</span>
      <button class="book">Book Now</button>
    </li>
  </ul>
</div>
Jimmy Bryant II
@JimBry
Jan 07 2015 16:03
It should be Discount in the string not tour
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:03
the HTML
@JimBry it's suppose to add in from the var tour
Jimmy Bryant II
@JimBry
Jan 07 2015 16:04
var message = $("<span>Call 1-555-jquery-air for a $" + discount + " discount.</span>");
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:04
but i'll try that
Maxim Orlov
@Maximization
Jan 07 2015 16:04
ah I see, your append code is incomplete
Jimmy Bryant II
@JimBry
Jan 07 2015 16:05
I'm sorry disregard that haha
Maxim Orlov
@Maximization
Jan 07 2015 16:05

the description says

Refactor your code so that the closest tour element is set to a variable named tour, and then use that variable instead

Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:05
i did do that
created the variable
Maxim Orlov
@Maximization
Jan 07 2015 16:06
take another louk at your tour variable
what do you actually initialize it to?
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:06
line?
Maxim Orlov
@Maximization
Jan 07 2015 16:06
var tour = $(this).closest(".tour").data("discount");
see what's wrong here
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:07
everything seems good
Maxim Orlov
@Maximization
Jan 07 2015 16:08
your tour variable should be equal to the closes tour element, nothing more
in your code you're combining two variables that should be separated, discount and tour
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:10
i'm creating the variable tour(as asked) $(this) to the closest tour element and saving discount from the data attribute
I can't seem to find the problem
Maxim Orlov
@Maximization
Jan 07 2015 16:12
take a look at this code and compare to yours, try to understand what's going on
$(document).ready(function(){
  $("button").on("click", function(){
    var tour = $(this).closest(".tour");
    var discount = tour.data("discount");
    var message = $("<span>Call 1-555-jquery-air for a $" + discount + " discount.</span>");
    tour.append(message);
    $(this).remove();
  });
});
Jimmy Bryant II
@JimBry
Jan 07 2015 16:12
Don't be afraid to use the hint it leads you to the problem
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:13
your getting the data attribute from the var tour
your code worked
huh
@Maximization can you explain a bit?
I'm not getting much from the video
Maxim Orlov
@Maximization
Jan 07 2015 16:15
take a look at the starting code for this question and compare to the above
$(document).ready(function(){
  $("button").on("click", function(){
    var discount = $(this).closest(".tour").data("discount");
    var message = $("<span>Call 1-555-jquery-air for a $" + discount + " discount.</span>");
    $(this).closest(".tour").append(message);
    $(this).remove();
  });
});
Jimmy Bryant II
@JimBry
Jan 07 2015 16:16
This is explained in the hint
Michael D. Johnson
@CodeNonprofit
Jan 07 2015 16:16
It looks like what you did was instead of declaring a new Var for tour, you modified the discount one
leaving you with no discount var
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:17
ah.. i get it
thanks everyone for the help
Maxim Orlov
@Maximization
Jan 07 2015 16:17
only difference is that all occurrences of $(this).closest(".tour") have been extracted from the code and put into a variable named tour and replaced by this variable instead
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 16:17
appreciate it
Ian Yeager
@iyeager2004
Jan 07 2015 16:21
function NumberSearch(str) { 

  var alphaCount = str.replace(/[^a-zA-Z]/g, "").length;
  var numSum = str.replace(/[^0-9]/g, "")
      .toString(10).split("")
      .map(function(t){return parseInt(t)})
      .reduce(function (prevVal, currVal, index, array) {
    return prevVal + currVal;
  });

  return Math.floor((numSum/alphaCount) + .5); 

}
A buddy and I just came up with this answer for Coderbyte 'Number Search' medium challenge. It works, but I'm trying to understand standard coding conventions. Can you ever chain too many functions?
And, does this leave things hanging around in memory? Obviously numSum starts as a string, but than is cast as an array of strings, an array of ints, and finally back down to an int. Is that healthy or should I declare those intermediate values?
Also, one more thing, in the background, is it actually doing a cast on the same variable, or does chaining really just obfuscate the creation of new variables, and at the end of operation there are four new variables hanging around?
Zachary G.
@BitTinker
Jan 07 2015 16:29
@AmmarAliShah @Maximization Glad to see I'm not the only one who is finding some of the CodeSchool jQuery tutorials a bit...confusing.
Had to back up and start from the beginning once I got to the level 4 stuff to make sure I was actually understanding what I was doing.
Maxim Orlov
@Maximization
Jan 07 2015 16:34
@iyeager2004 good questions, nobody will prohibit you from chaining too much functions, but you have to be careful since your code can become messy really quick
I'm not sure if JS does any form of garbage collection, however creating variables for all the intermediary stuff will definitely make sure you're taking up memory
although memory is not something you have to worry about, instead focus on readability
would you consider your code self-explanatory? what are the complex blocks that could be abstracted out?
Ian Yeager
@iyeager2004
Jan 07 2015 16:38
@Maximization Yeah, that's mostly what I was concerned about. I try to maintain clear and concise code on the projects I do while working, and comment where I can. I just wanted to make sure I was following a standard (btw, know of any books that put out a 'standard' coding look and feel for JavaScript?)
I think declaring the intermediate int array would probably make it more readable. Than you can see reduce is working on an array of ints.
Maxim Orlov
@Maximization
Jan 07 2015 16:47
@iyeager2004 if you're interested about conventions take a look at https://github.com/airbnb/javascript
you'll get a general idea of how to write your JS code
I'm sure there are plenty of books out there since this is a commonly discussed topic, I don't know any good ones though since I haven't done any research on that
Ian Yeager
@iyeager2004
Jan 07 2015 16:51
Cool thanks.
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:01
i have a questoin about constructor notation, I'm on challenge #30
from the challenge itself, this code is used:
var bob = new Object();
does Object have to be the word object? is bob the object or is bob an object of "Object"? why isn't it new Person(); ? Would that work too?
Branden Byers
@brandenbyers
Jan 07 2015 19:04
Do you already have a Person constructor declared? If so, then I would think var bob = new Person(); is fine.
but if you haven’t defined Person then it wouldn’t.
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:05
No I don't, this is just working from scratch. I am just wondering of "Object" in the tutorial code like I pasted above has to be exactly that. is that why it works, because we are reating objects?
it may seem like a silly question but if you can create a new object with efewer keystrokes (var bob = {};) why would you use the Ojbect notation?
Branden Byers
@brandenbyers
Jan 07 2015 19:05
The code above you are creating an Object called bob.
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:06
OK, see my question above, I'll repeat it here:
if you can create a new object with efewer keystrokes (var bob = {};) why would you use the Ojbect notation?
Branden Byers
@brandenbyers
Jan 07 2015 19:06
var bob = new Object(); is the same as ’var bob = {};
You can use either.
But if you want to use var bob = new Person(); then you will first need to define var Person = {}’
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:08
OK, that makes sense.
Branden Byers
@brandenbyers
Jan 07 2015 19:08
Am I making any sense of what your asking?
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:08
yes. I guess I can't figure out why you'd use new Object when you can just use {} except for readability of you have more complex objects to work with.
Charles Watson
@sircharleswatson
Jan 07 2015 19:09
@AdventureBear There are lots of things you can do with Object() that you can't do with {}
Ryan Malm
@Rybar
Jan 07 2015 19:09
I think they introduce it that way to introduce you to the constructor pattern without having to show how to build a constructor. In practice you're right, you'd never really use it that way, is cleaner to use object literal.
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:09
Like Person->Manager->Product Manager might all be types of objects, and would make sense... var Sam = new ProductManager(); for example. (and is also a Manager and a Person). Is that thinking along the right lines or am I making it too complicated
@sircharleswatson that's good to know. @Rybar I can see that too. I just want to know where to "file" this Object constructor thing in my mind. :)
Branden Byers
@brandenbyers
Jan 07 2015 19:11
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:12
oh great, thanks for that link!
Charles Watson
@sircharleswatson
Jan 07 2015 19:12
Constructors need to be executed. That means they will be slower, but you can easily add some validation code etc. to them, and they allow the construction of complex objects with public, privileged methods and private "attributes" hidden in the constructors scope. Also, they of course construct objects that share a prototype, which you might find useful.
(the "slower" part won't make much of a difference at this point)
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:12
oooh...that sounds pretty cool @sircharleswatson ...and makes sense now.
Branden Byers
@brandenbyers
Jan 07 2015 19:13
Plus, as far as I remember, Crockford doesn’t like “new,” so there’s another reason not to use them if you don’t have to.
Ryan Malm
@Rybar
Jan 07 2015 19:31
I found this answer to be useful re: new keyword, and in reminding me to not take one man's extremely opiniated view of coding practices as gospel: http://stackoverflow.com/a/383503
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:37
aha! now the question is addressed for me, the challenge is describing writing constructors, and calls the "Object" constructor boring, lol.
Branden Byers
@brandenbyers
Jan 07 2015 19:41
@Rybar I agree. Crockford is an opinionated character. But when in doubt, I generally refer to Crockford because he generally has reasonably good reasons to back up his opinions. And sometimes it is just easier to go with his opinionated stance than some of the garbage that can be made of javascript.
Ammar Ali Shah
@AmmarAliShah
Jan 07 2015 19:43
I really need a plan to study Physics and Chemistry
Suzanne Atkinson
@AdventureBear
Jan 07 2015 19:45
function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
}

var sally = Person("Sally Bowles", 39);
var holden = Person("Holden Caulfield", 16);

console.log("sally's species is " + sally.species + " and she is " + sally.age );
console.log("holden's species is " + holden.species + " and he is " + holden.age);
When I run this code in codecademy challenge "create an address book", I get the error" Your code threw the following error when sally's name was checked: TypeError: Cannot read property 'name' of undefined"
AH never mind
found it while explaining the problem...i did not use the word "new"
sometimes explaing the problem helps solve the problem
Ryan Malm
@Rybar
Jan 07 2015 19:55
oh absolutely @brndnb I agree, I love crockford, and his best practices are on the whole awesome, but some things are just personal preference, have no bearing on performance or clarity
clarity also being something that's partly subjective
see, now that you've created your own object constructor @AdventureBear the whole x = new Thing() syntax makes more sense, yes?
Branden Byers
@brandenbyers
Jan 07 2015 19:58
Yes, I even tried to adhere to JSLint for a while. But even though I understand his preference, it was still felt ackward to always define var = i; outside of loop parameters. But now that I’ve switched to JSHint, now I find myself declaring the variables Crockford’s way without even thinking about it!

As in:

var = i; for(i = 0; i < 10; i +=1)

versus

for(i=0; i< 10; i++)

Ryan Malm
@Rybar
Jan 07 2015 20:01
`for(var i....
but yeah I get what you mean
Branden Byers
@brandenbyers
Jan 07 2015 20:02
or that!
Alex Hazel
@metalhaze
Jan 07 2015 20:02
also I heard that defining the length of a variable that your iterating over is also favorable since you aren't making that calculation each time
var names = [1,2,3,4,5];
var namesLength = names.length;
var i = 0;

for(i; i < namesLength; i++) {

}
Ryan Malm
@Rybar
Jan 07 2015 20:03
This message was deleted
This message was deleted
lol
Suzanne Atkinson
@AdventureBear
Jan 07 2015 20:04
@Rybar sure does!!! I just needed a few more examples. :) maybe it'd be more clear if they introduced it first with an example of how it's more powerful than var = {};
Evan Davis
@davisec52
Jan 07 2015 20:47
I've been trying to write a simple function comparing a parameter, which is a string, against an array of letters and then list the letters in common. However, I can't get it work. In the code below, console.log will print the letters in common, but return only produces "a". The code is also pretty verbose. I'd appreciate any advice on how to get it to return the proper result and how to simplify it.
function listLetters(letters) {
    lettersArray = letters.split("");
    var alphabet = ("abcdefghijk").split("");
    list = [];
    for(var i = 0; i < alphabet.length; i++) {
        for(var j = 0; j < letters.length; j++) {
            if (lettersArray[j] === alphabet[i]) {
                list += lettersArray[j];
                return list;
            }
        }
    }
}
listLetters("abcxyz");
This message was deleted
Maxim Orlov
@Maximization
Jan 07 2015 21:08
@davisec52 you need to insert the return statement outside and after the loop
otherwise the loop only iterates once and returns the first letter
Evan Davis
@davisec52
Jan 07 2015 21:11
I've tried the return statement in all locations outside and I still get a wrong result. I've been using the labs.codecademy.com as testing ground, if that makes any difference.
I also changed letters.length to lettersArray.length.
Maxim Orlov
@Maximization
Jan 07 2015 21:19
jesus I can't find a decent website to test javascript code
Ben Stoltz
@benstoltz
Jan 07 2015 21:19
other then the console?
Evan Davis
@davisec52
Jan 07 2015 21:19
I get weird results
Kathy O'Driscoll
@odrisck
Jan 07 2015 21:20
repl.it or jsfiddle not working for you?
Maxim Orlov
@Maximization
Jan 07 2015 21:20
yeah, console is not nice to work in
Evan Davis
@davisec52
Jan 07 2015 21:20
right, no matter where I place the return statement, I get a wrong result.
Maxim Orlov
@Maximization
Jan 07 2015 21:20
jsfiddle not doing anything
Evan Davis
@davisec52
Jan 07 2015 21:20
Is the code ok?
I just thought I had errors in the code.
Maxim Orlov
@Maximization
Jan 07 2015 21:20
ok repl is nice
Kathy O'Driscoll
@odrisck
Jan 07 2015 21:21
there you go :)
Ben Stoltz
@benstoltz
Jan 07 2015 21:21
I’ll take a look at the code @davisec52
Maxim Orlov
@Maximization
Jan 07 2015 21:21
let me check
Kathy O'Driscoll
@odrisck
Jan 07 2015 21:21
ok grabbing some foodstuffs brb
Ben Stoltz
@benstoltz
Jan 07 2015 21:21
One thing @davisec52 just for future reference, I’d preface all your variables with var.
Evan Davis
@davisec52
Jan 07 2015 21:22
Thank you. I'm o n repl.it, but it appear only for python.
Jordan Wood
@jordanw
Jan 07 2015 21:22
function listLetters(letters) {
    var lettersArray = letters.split("");
    var alphabet = ("abcdefghijk").split("");
    list = [];
    for(var i = 0; i < alphabet.length; i++) {
        for(var j = 0; j < letters.length; j++) {
            if (lettersArray[j] === alphabet[i]) {
                list += lettersArray[j];

            }
        }
    } return list;
} 
listLetters("abcxyz");
Evan Davis
@davisec52
Jan 07 2015 21:22
@benstoltz Yow, right. need "var"
Maxim Orlov
@Maximization
Jan 07 2015 21:22
you don't need var per se
Ben Stoltz
@benstoltz
Jan 07 2015 21:22
that won’t break it.
@Maximization Realistically working on a project/production code yes you should be using Var.
Maxim Orlov
@Maximization
Jan 07 2015 21:23
like I told you before, you need to have the return statement outside of the loop, at the end of the function
I'm just saying that var or no var that's not what was breaking his code
Evan Davis
@davisec52
Jan 07 2015 21:23
@Maximization Yes, tried that and it did not work.
Ben Stoltz
@benstoltz
Jan 07 2015 21:23
Oh okay @Maximization
Maxim Orlov
@Maximization
Jan 07 2015 21:23
function listLetters(letters) {
    lettersArray = letters.split("");
    var alphabet = ("abcdefghijk").split("");
    list = [];
    for(var i = 0; i < alphabet.length; i++) {
        for(var j = 0; j < letters.length; j++) {
            if (lettersArray[j] === alphabet[i]) {
                list += lettersArray[j];
            }
        }
    }
    return list;
}
listLetters("abcxyz");
Jordan Wood
@jordanw
Jan 07 2015 21:24
Yeah once you hit that return statement it will break out of the loop.
Maxim Orlov
@Maximization
Jan 07 2015 21:24
only thing I changed from your code is put the return statement at the end, returns abc just as expected
Ben Stoltz
@benstoltz
Jan 07 2015 21:24
well for me, when I move the return list to the bottom it returns abc
Maxim Orlov
@Maximization
Jan 07 2015 21:25
@davisec52 what answer were you expecting?
Evan Davis
@davisec52
Jan 07 2015 21:25
@Maximization with the parameter above (abcxyz), the function should return abc.
Ben Stoltz
@benstoltz
Jan 07 2015 21:26
So it works.
Are you wanting it to return a string or an array btw?
Evan Davis
@davisec52
Jan 07 2015 21:27
@benstoltz Yes! just tried and it is--strange, because I tried return in that locationl
Ben Stoltz
@benstoltz
Jan 07 2015 21:27
Cause’ atm you are having it return a string
Evan Davis
@davisec52
Jan 07 2015 21:27
I copied the code above pasted into labs.
Ben Stoltz
@benstoltz
Jan 07 2015 21:27
But originally you initialize list as an array
Evan Davis
@davisec52
Jan 07 2015 21:28
Right now, it doesn't matter if it returns as a string or array.
I was more concerned with getting the correct result.
Ben Stoltz
@benstoltz
Jan 07 2015 21:28
okay
Evan Davis
@davisec52
Jan 07 2015 21:28
Thank you very much!
Ben Stoltz
@benstoltz
Jan 07 2015 21:28
np
Evan Davis
@davisec52
Jan 07 2015 21:29
actually, yes, shouldn't the list array have returned as an arrary?
Ben Stoltz
@benstoltz
Jan 07 2015 21:32
In this case no.
Evan Davis
@davisec52
Jan 07 2015 21:32
Why?
Ben Stoltz
@benstoltz
Jan 07 2015 21:32
The way it’s written is you’re adding a string item to the variable list.
Rather then appending an item to an array.
So in JS you can strings to strings, and they are just concatenated.
Maxim Orlov
@Maximization
Jan 07 2015 21:34
when you declare a variable in JS, it's type is not fixed
Evan Davis
@davisec52
Jan 07 2015 21:34
Since both alphabet and lettersArray are arrays, I thought list array would return as an arrary.
Maxim Orlov
@Maximization
Jan 07 2015 21:34
so although list is first an empty array it turns into a string afterwards because you are concatenating letters
Ben Stoltz
@benstoltz
Jan 07 2015 21:34
So what’s happening is:
1) list the variable is created
2) list is assigned an empty array
3) your line list += lettersArray[j] overwrites the empty array assigning it the string ‘a'
Evan Davis
@davisec52
Jan 07 2015 21:34
@Maximization So a variable starting out as an array could change to a string if I'm careless.
Ben Stoltz
@benstoltz
Jan 07 2015 21:35
and then ‘ab'
‘abc'
Yes.
Maxim Orlov
@Maximization
Jan 07 2015 21:35
list.push(lettersArray[j]); would get you an array
Ben Stoltz
@benstoltz
Jan 07 2015 21:35
It could become anything if you’re not careful
Maxim Orlov
@Maximization
Jan 07 2015 21:35
@davisec52 yes
Evan Davis
@davisec52
Jan 07 2015 21:35
Then I could have simply stated var list = "";
or perhaps just var list;
Maxim Orlov
@Maximization
Jan 07 2015 21:36
you have to set it equal to something
Evan Davis
@davisec52
Jan 07 2015 21:36
@Maximization Thank you
Ben Stoltz
@benstoltz
Jan 07 2015 21:36
It’s actually (performance) wise better to initialize a variable when you declare it
Maxim Orlov
@Maximization
Jan 07 2015 21:36
otherwise it's going to stay undefined
Ben Stoltz
@benstoltz
Jan 07 2015 21:36
You can let it sit as a var list;, but then if you want it to be an array…you run into errors
Evan Davis
@davisec52
Jan 07 2015 21:37
@benstoltz Ok. Understand.
Ben Stoltz
@benstoltz
Jan 07 2015 21:37
You can create it and then assign say things to it later, but when you’re doing something like appending to an array it’ll wonk out on you.
Evan Davis
@davisec52
Jan 07 2015 21:39
@Maximization I just added the statement list.push(lettersArray[j]) and got back undefined is not a function.
seahik
@seahik
Jan 07 2015 21:40
@benstoltz It could become anything if you’re not careful
Maxim Orlov
@Maximization
Jan 07 2015 21:40
how does your declaration of list look like
Evan Davis
@davisec52
Jan 07 2015 21:40
function listLetters(letters) {
    lettersArray = letters.split("");
    var alphabet = ("abcdefghijk").split("");
    var list = [];
    for(var i = 0; i < alphabet.length; i++) {
        for(var j = 0; j < lettersArray.length; j++) {
            if (lettersArray[j] === alphabet[i]) {
                list += lettersArray[j];
                list.push(lettersArray[j]);
            }
        }
    }
    return list;
}
listLetters("abcxyz");
Maxim Orlov
@Maximization
Jan 07 2015 21:40
get rid of list += lettersArray[j];
Ben Stoltz
@benstoltz
Jan 07 2015 21:40

@seahik Yeah, from what I understand if you don’t assign a value to a variable immediately you should do something like:

var frank = undefined;

But honestly in my mind it makes more sense to just assign a value immediately
Evan Davis
@davisec52
Jan 07 2015 21:41
same result:
function listLetters(letters) {
    lettersArray = letters.split("");
    var alphabet = ("abcdefghijk").split("");
    var list = [];
    for(var i = 0; i < alphabet.length; i++) {
        for(var j = 0; j < lettersArray.length; j++) {
            if (lettersArray[j] === alphabet[i]) {
                list = lettersArray[j];
                list.push(lettersArray[j]);
            }
        }
    }
    return list;
}
listLetters("abcxyz");
Maxim Orlov
@Maximization
Jan 07 2015 21:41
get rid of the whole line
Evan Davis
@davisec52
Jan 07 2015 21:41
yup
Maxim Orlov
@Maximization
Jan 07 2015 21:41
function listLetters(letters) {
    lettersArray = letters.split("");
    var alphabet = ("abcdefghijk").split("");
    var list = [];
    for(var i = 0; i < alphabet.length; i++) {
        for(var j = 0; j < lettersArray.length; j++) {
            if (lettersArray[j] === alphabet[i]) {
                list.push(lettersArray[j]);
            }
        }
    }
    return list;
}
listLetters("abcxyz");
Ben Stoltz
@benstoltz
Jan 07 2015 21:41
Why do you still have the line: “list = lettersArray[j];
^ what @Maximization did
Ryan Malm
@Rybar
Jan 07 2015 21:42
yup; you're reassigning list to the value in lettersarray[j], then attempting to push to an array that no longer exists
Evan Davis
@davisec52
Jan 07 2015 21:42
@Maximization @benstoltz @Rybar That worked.
seahik
@seahik
Jan 07 2015 21:42
@benstoltz i just thought that statement has a nice ring to it and deserved to be restated It could become anything if you’re not careful
Evan Davis
@davisec52
Jan 07 2015 21:43
and why did I not get rid of the line? I'm pretty green at this.
Ben Stoltz
@benstoltz
Jan 07 2015 21:43
@seahik hahaha
It could become a unicorn!
seahik
@seahik
Jan 07 2015 21:43
haha or a weresquirrel
Evan Davis
@davisec52
Jan 07 2015 21:44
@benstoltz @seahik Code is all about transformation
Ben Stoltz
@benstoltz
Jan 07 2015 21:44
weresquirrel…/shudders….horrifying thought
Evan Davis
@davisec52
Jan 07 2015 21:44
rabbits with fangs
seahik
@seahik
Jan 07 2015 21:44
don't eat nuts or forget to brush your teeth. Proven correlation to turning into a weresquirrel (according to EJS)
oops sorry here I am interrupting the help chat...carry on
Evan Davis
@davisec52
Jan 07 2015 21:46
Thank you everybody--I appreciate the help can clarification!
Ben Stoltz
@benstoltz
Jan 07 2015 22:07
No problem @davisec52 :) we are here to help!