These are chat archives for FreeCodeCamp/Help

7th
Apr 2015
Evan Davis
@davisec52
Apr 07 2015 00:15
@AdventureBear With camel case, you have "thisIsSpinalTap". So the issue is how to insert a space between [a-z] and [A-Z]. You capture the group by using parens. In your example above, you have parens and brakets reversed. You want ([a-z])([A-A]). You also want to use the global (g) indicator. To get the space, try using the $1 $2 flags shown in the example from the MDN link. In this instance, $1 would represent ([a-z]) and $2 would represent ([A-Z]).
Evan Davis
@davisec52
Apr 07 2015 00:24

I'm working on another one of the nodeschool expressworks challenges, and again I don't fully understand how the solution works and am looking for a little help.

The goal is to list how many times each string element occurs in
the array. This is similar to one of the coderbytes, but
using the reduce method.

I'd appreciate help understanding the function of 1 in the
return statement, as well as the additional {} in the return.

function countWords(arr) {
    var objOfArr = {};
    var temp = [];

    return arr.reduce(function(a, b) {
        a[b] = ++a[b] || 1;  //<== What is the function of the 1?
        return a;
    }, {} );  //<== What is the function of the additional {}?

}
countWords(["Apple", "Banana", "Apple", "Durian", "Durian", "Durian"]);
Ben McMahon
@benmcmahon100
Apr 07 2015 00:27
@terakilobyte I think this is one for you! :P
@davisec52 I think the 1 is for "the number of occurances or it only occurs once
I'm not sure what the empty object is though!
Nathan
@terakilobyte
Apr 07 2015 00:29
when you use reduce like this
you are creating an object with keys that were the values of the old array
it assigns 1 to the key a[b] if a[b] is null or undefined
Evan Davis
@davisec52
Apr 07 2015 00:30
@terakilobyte Thank you.
Nathan
@terakilobyte
Apr 07 2015 00:30
passing in the empty object lets it reduce like that, as the empty object is the starting value of the reduction (and quite right)
Ben McMahon
@benmcmahon100
Apr 07 2015 00:30
^^ much smarter guy than I am :p
Nathan
@terakilobyte
Apr 07 2015 00:31
vicious rumors
Ben McMahon
@benmcmahon100
Apr 07 2015 00:31
Is was right about the one though! :D
Nathan
@terakilobyte
Apr 07 2015 00:31
:)
Evan Davis
@davisec52
Apr 07 2015 00:31
@terakilobyte So, in a sense, everything before the empty obj represents a, and the empty obj represents b.
Nathan
@terakilobyte
Apr 07 2015 00:31
and now you see another way to use reduce
not only can you pack up an array into one value
but you can convert an array to an object
and, with a little imagination, vice versa
Ben McMahon
@benmcmahon100
Apr 07 2015 00:32
And harsher truths!
Nathan
@terakilobyte
Apr 07 2015 00:32
no, the empty object in this case is a
that way you’re guaranteed to start assigning key/value pairs
remember, javascript objects are really just dictionaries
you wouldn’t want to reduce an object into an existing value (a)
Ben McMahon
@benmcmahon100
Apr 07 2015 00:33
Unless you construct no?
Like the creating a person bonfire
Nathan
@terakilobyte
Apr 07 2015 00:34
well if you’re using reduce like this you won’t really be using a constructor
the idea is you want a unique object that represents the old array
you could pass in an existing object
Ben McMahon
@benmcmahon100
Apr 07 2015 00:34
Yeah it would be weird reducing functions :p
Evan Davis
@davisec52
Apr 07 2015 00:34
@terakilobyte Right. I didn't understand the construction. I wasn't able to get there from the MDN info.
Nathan
@terakilobyte
Apr 07 2015 00:34
but doing so is unsafe
empty object means no overwritten keys
and checking if a key exists inside the reduction funtion is doing too much work, imo
your functions should do 1 thing very well
if they do more, you need another function
it will reduce errors, make debugging easier, and ultimately allow you to extend further
Ben McMahon
@benmcmahon100
Apr 07 2015 00:37
I wonder can we just have a terakilobyte app for constant reference! :p
Evan Davis
@davisec52
Apr 07 2015 00:38
@terakilobyte @benmcmahon100 Thank you for the assist!
Ben McMahon
@benmcmahon100
Apr 07 2015 00:38
Anytime! Although I'd highly question my use!
Suzanne Atkinson
@AdventureBear
Apr 07 2015 00:48
@davisec52 thanks, the a-zA-z makes more sense because I'm looking for that back to back combination. I still can't get the capture groups to insert:
  var reg = /([a-z])([A-Z])/g;
  var camelCaseSplit = str.replace(reg, $1 + " " + $2);
Am I using them correctly? I'm not seeing the example in MDN of how to use them
Nathan
@terakilobyte
Apr 07 2015 00:58
I believe capture groups are experimental
even worse
non-standard
Suzanne Atkinson
@AdventureBear
Apr 07 2015 01:00
right
Nathan
@terakilobyte
Apr 07 2015 01:00
though it looks like you should use RegExp.$1 + ‘ ‘ + RegExp.$2
Suzanne Atkinson
@AdventureBear
Apr 07 2015 01:00
thats what I found
oh, i'll try that
Nathan
@terakilobyte
Apr 07 2015 01:00
as it is implemented in all browsers
Suzanne Atkinson
@AdventureBear
Apr 07 2015 01:00
what's the p1 referencce then?
Nathan
@terakilobyte
Apr 07 2015 01:00
$1 will reference the first capture group, $2 the second, and so on
Elliescode
@EllieAdam
Apr 07 2015 01:13
@terakilobyte Hey! I I want you to take a look of my project pls.
@benmcmahon100 I need help on responsive layout.
Ben McMahon
@benmcmahon100
Apr 07 2015 01:17
I'm not sure if I'm the best man for the job but if it's a problem I can certainly take a look! :D
Although terakilobyte may be a better opinion to have :p
Elliescode
@EllieAdam
Apr 07 2015 01:22
Is he around?
Ben McMahon
@benmcmahon100
Apr 07 2015 01:24
I'm not sure! He be off for a while and then back on though.
Nathan
@terakilobyte
Apr 07 2015 01:24
I’m off an on
have dinner coming out of the oven in 9 minutes and then I can take a look
Elliescode
@EllieAdam
Apr 07 2015 01:26
9 min? I will take it.
Nathan
@terakilobyte
Apr 07 2015 01:26
the hospital has crap internet and crap cell phone reception to boot so can’t tether
Elliescode
@EllieAdam
Apr 07 2015 01:27
@terakilobyte I am so sorry about your wife's health.
I hope she will feel better soon.
Nathan
@terakilobyte
Apr 07 2015 01:28
I hope so too, and thanks
things are getting better, hoping she can come home from the hospital tomorrow or the day after
Elliescode
@EllieAdam
Apr 07 2015 01:28
Thats good.
Suzanne Atkinson
@AdventureBear
Apr 07 2015 01:29
got it
Elliescode
@EllieAdam
Apr 07 2015 01:29
At home she will feel much better.@terakilobyte
Nathan
@terakilobyte
Apr 07 2015 01:43
I think so too
ok do you have a link to the repo you want me to look at?
Elliescode
@EllieAdam
Apr 07 2015 01:58
@terakilobyte I am still here. Whenever you have time just text me in screen hero. I spend my entire day after this problem. thanks
Evan Davis
@davisec52
Apr 07 2015 03:37
@AdventureBear Great--looks like it is working. Did it pass the tests?
Suzanne Atkinson
@AdventureBear
Apr 07 2015 03:41
yes, it did! thanks for the help! struggling to solve puzzles is one thing, but struggling to get the right syntax is frustrating.
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:00
Any MySQL junkies out there who could help me with something?
Cristián Berríos
@crisberrios
Apr 07 2015 07:07
I know only basic SQL, if that helps
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:09
@crisberrios Depends how basic :P
Cristián Berríos
@crisberrios
Apr 07 2015 07:11
I get the query, but I couldn't tell what's wrong :(
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:12
The query should return the 'latest' president for each country, so either the current one or the last one if there isn't a current one
Bianca Mihai
@biancamihai
Apr 07 2015 07:13
@rpoddighe first you have where ... in (subquery that returns more than one result)
second you I don;t think you need to use where, you need to use group by and having
you try to get the last presidencies from each country?
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:16
@biancamihai yes
So the presidencies table entries belong to a user and a country, and have a start and end date
Bianca Mihai
@biancamihai
Apr 07 2015 07:17
then (I don't have the table strucutre to test it) but I think it you should replace the where with group by country_id having max(end_date) = end_date
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:17
@biancamihai That sounds like what I need, let me try it out
Bianca Mihai
@biancamihai
Apr 07 2015 07:18
;) let me know ...
why do you need INNER JOIN users ON users.id = presidencies.user_id ? don;t you have the president data in the presidencies table?
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:20
@biancamihai No just the duration, I keep the data separate because a user can easily have multiple presidencies
Also for different countries
Bianca Mihai
@biancamihai
Apr 07 2015 07:20
:) ok, I was just asking :P
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:22
Hehe, thanks for helping :)
Bianca Mihai
@biancamihai
Apr 07 2015 07:23
working? I love queries ;)))
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:23
It doesn't seem to work... Can you actually use HAVING like this?
I thought HAVING is used like this: HAVING 'column' 'operator' 'value'
e.g. HAVING end_date > 2015-01-01
Bianca Mihai
@biancamihai
Apr 07 2015 07:25
you can use it with an aggregate function https://msdn.microsoft.com/en-us/library/ms180199.aspx
but do you get an error or the result is not right?
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:26
No error, just an empty result
Bianca Mihai
@biancamihai
Apr 07 2015 07:27
and if you put your query without having do you get results?
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:27
Yes it behaves as expected
Bianca Mihai
@biancamihai
Apr 07 2015 07:28
can you post 1-2 results that you get?
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:30
37 1428422400 1428379199 CAN 10205017476972498 NULL Canada (etc..)
47 1428408000 1428429599 RUS 10205017476972498 NULL Russia (etc...)
Bianca Mihai
@biancamihai
Apr 07 2015 07:31
k, let me test this :) 5 minutes
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:38
Sure :D
Bianca Mihai
@biancamihai
Apr 07 2015 07:54
@rpoddighe I simplified a bit :)
created a table test
MariaDB [swipeTapSell]> select * from test;
+----+--------+------------+---------+
| id | name | end_date | country |
+----+--------+------------+---------+
| 1 | Name 1 | 2000-02-01 | 1 |
| 2 | Name 2 | 2000-02-03 | 2 |
| 3 | Name 3 | 2000-05-03 | 2 |
| 4 | Name 4 | 2000-10-03 | 2 |
| 5 | Name 5 | 2002-10-03 | 1 |
+----+--------+------------+---------+
and the query
MariaDB [swipeTapSell]> select test.name, test.end_date from test where end_date in (select max(t.end_date) as max_date from test t where country = t.country group by country);
+--------+------------+
| name | end_date |
+--------+------------+
| Name 4 | 2000-10-03 |
| Name 5 | 2002-10-03 |
+--------+------------+
2 rows in set (0.00 sec)
hope it helps :)
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:55
Awesome, let me try it out!
It works! Thanks so much!
Bianca Mihai
@biancamihai
Apr 07 2015 07:58
You are welcome :), hard when you don;t know all the DB schema but we managed in the end ;)))
Renzo Poddighe
@rpoddighe
Apr 07 2015 07:58
Yes, even more kudos to you
Bianca Mihai
@biancamihai
Apr 07 2015 07:59
@rpoddighe one more thing :) don;t use select * :)
in production you will kill the db ;)))
get only what you need from the db
Renzo Poddighe
@rpoddighe
Apr 07 2015 08:00
@biancamihai Yes it's still in development and the db schema is constantly changing but I'll make sure to remove the * in production
Bianca Mihai
@biancamihai
Apr 07 2015 08:09
@rpoddighe more simple select distinct country, test.name, test.end_date from test where end_date in (select max(t.end_date) as max_date from test t group by t.country); :)
and I should get back to work now :P
Renzo Poddighe
@rpoddighe
Apr 07 2015 08:10
Amazing :) thanks for the trouble
Serhii Borovskyi
@serhiicss
Apr 07 2015 15:48
I am at "Factorialize a Number" bonfire http://www.freecodecamp.com/bonfires/factorialize-a-number
function factorialize(num) {
  if (num === 0) {
    return 1;
  } else {
  return  num * factorialize(num - 1); 
  }
}

factorialize(5);
I don't understand how this solution runs 5 times 1x2x3x4x5
Sudeepto
@Sudeepto
Apr 07 2015 15:50
@serhiicss Also take the case when n = 1 . You are taking only one base case when n = 0
1! = 0! = 1
Serhii Borovskyi
@serhiicss
Apr 07 2015 15:50
@Sudeepto yes, true, but if 1 is entered, then would not it be 1x1=1 anyway?
Can you tell me how does it multiple multiplications without a loop?
Sudeepto
@Sudeepto
Apr 07 2015 15:52
@serhiicss wait up.
Ben McMahon
@benmcmahon100
Apr 07 2015 15:52
@serhiicss you can just have
var resuts = [1,num];
var i = 2;
while(i<num){
if(num%i == 0){
results.push(i);
}
i++;
}
Sudeepto
@Sudeepto
Apr 07 2015 15:53
function factorialize(num) {
  if(num === 1 || num === 0) {
    return 1 ;
  }
  else {
    return factorialize(num - 1) * num ;
  }
}

factorialize(5);
Ben McMahon
@benmcmahon100
Apr 07 2015 15:53
as 1 is alsways going to be a factor and the number itself
Sudeepto
@Sudeepto
Apr 07 2015 15:54
@serhiicss You missed the case when num = 1
Ben McMahon
@benmcmahon100
Apr 07 2015 15:55
you can just add the one straight to the results as every whole number can have one divided evenly! :p
mshirishkumar
@mshirishkumar
Apr 07 2015 15:55
function factorialize(num) {
  if(num > 1) {
    return factorialize(num - 1) * num ;
  }
}

factorialize(5);
Serhii Borovskyi
@serhiicss
Apr 07 2015 15:56
My question is how this part return factorialize(num - 1) * num ; runs multiple times?
Ben McMahon
@benmcmahon100
Apr 07 2015 15:57
in my solution it's a loop
Serhii Borovskyi
@serhiicss
Apr 07 2015 15:57
@benmcmahon100 yes, but how does it work in my solution?
Ben McMahon
@benmcmahon100
Apr 07 2015 15:57
the other solutions use a recursive loop
the function calls itself untill a certain condition is met
Serhii Borovskyi
@serhiicss
Apr 07 2015 15:58
ah I see
Sudeepto
@Sudeepto
Apr 07 2015 15:58
wait up . I am writing :smile:
Serhii Borovskyi
@serhiicss
Apr 07 2015 15:59
Is factorialize a JS funciton?
Sudeepto
@Sudeepto
Apr 07 2015 15:59

@serhiicss Its recursion. The function calls itself up finite number of times and in a way reduces itself on the way.

So for factorialize(5)

num = 5 , which is neither 1 nor 0. So the else clause will execute.
factorialize(4) * 5

Serhii Borovskyi
@serhiicss
Apr 07 2015 15:59
I mean like reserved function
Ben McMahon
@benmcmahon100
Apr 07 2015 15:59
no
i don't even think it's a real word :p
Serhii Borovskyi
@serhiicss
Apr 07 2015 16:00
@Sudeepto I see, I dont know what recursion is apparently :)
@benmcmahon100 Yeah, I just made it up, patent pending :)
Ben McMahon
@benmcmahon100
Apr 07 2015 16:01
@serhiicss I like it! You can use a for/while loop as well it doesn't really change the functionality
Serhii Borovskyi
@serhiicss
Apr 07 2015 16:01
Thanks guys, I was looking for the solution on google and found the loop one and recursion and was wondering how the latter works
Sudeepto
@Sudeepto
Apr 07 2015 16:01
Similarly 4 is not 1 or 0 . So again the else clause runs factorialize(3) * 4 . But remember the 5 is still in the stack waiting to be returned !!
So in a way, you just wrote, factorialize(3) * 4 *5 . And it goes on until we hit the Base Case i.e. when num = 1 or num = 0.
In that case we just return 1.
Ben McMahon
@benmcmahon100
Apr 07 2015 16:02
I'm sure there's an argumnet the recursion is better in this case as you don't waste memory registering an iterator like i
but the difference is miniscule! :D
Serhii Borovskyi
@serhiicss
Apr 07 2015 16:03
Great, thanks yall. I am going to read up on recursion.
VonRichtoffen
@VonRichtoffen
Apr 07 2015 17:21
this is an interesting discussion.
I am a bit confused about where the succeeding values the recursion produces are stored
For instance when you return something and is not assigned to a variable, where does that value live?
Ben McMahon
@benmcmahon100
Apr 07 2015 17:23
the return passes the value back to whatever called it
the best way to understand is to go to that bonfire, paste the code it and console.log(); then value before it returns to see the steps it takes
VonRichtoffen
@VonRichtoffen
Apr 07 2015 17:25
yes, good idea. Let me play with it that way
Suzanne Atkinson
@AdventureBear
Apr 07 2015 17:25
if there are any mongo/node experts here, I need help. I'd my code runs in @crisberrios environemnt, but on my environment I get an error when i try to save a document back to the database within my app.js. very frustrating.
Can anyone pair later today? (I actually have to leave in 15 min, but will be/can be back later)
@QuincyLarson ? @terakilobyte ?
I'm doing the mongo university and was unable to get homework 3-1 to pass on my system. :(
Nathan
@terakilobyte
Apr 07 2015 18:59
I can take a look later @AdventureBear
Ben McMahon
@benmcmahon100
Apr 07 2015 19:04
@terakilobyte hey if you're still around is T = 2\pi\sqrt{a^3/\mu} the right formual for http://www.freecodecamp.com/bonfires/map-the-debris ?
Nathan
@terakilobyte
Apr 07 2015 19:08
at a glance, no
2 Pi sqrt(a^3/mu)
if memory serves me
Ben McMahon
@benmcmahon100
Apr 07 2015 19:13
Thanks! that's the same one but copy and paste mucked it up! :p
Nathan
@terakilobyte
Apr 07 2015 19:14
it happens
good luck with it, that’s one of the most fun bonfires I think as it’s real world
the data isn’t made up
and yeah I've used it in physics! :D
Nathan
@terakilobyte
Apr 07 2015 19:16
I’m thinking about adding a level 4 or 5 challenge that will have you calculate phase angles and how long an interplanetary transfer would take at optimal exit from Earth to Venus
Ben McMahon
@benmcmahon100
Apr 07 2015 19:16
I'm thinking you're gonna have to seperate the maths ones! :p
I feel sorry for people who really struggle with maths
(more than i do)
Elliescode
@EllieAdam
Apr 07 2015 19:17
lol
Ben McMahon
@benmcmahon100
Apr 07 2015 19:19
@terakilobyte can you see anything wrong with this?
res = 2*(Math.PI)*(Math.sqrt((Math.pow(arr.avgAlt, 3)/GM)));
Nathan
@terakilobyte
Apr 07 2015 19:19
yep
you arent’ accounting for the radius of the earth it looks like
Ben McMahon
@benmcmahon100
Apr 07 2015 19:19
ah of course!
@terakilobyte it's still being a bit gammy :p
  var height = arr.avgAlt-earthRadius; 
 res = 2*(Math.PI)*(Math.sqrt((Math.pow(height, 3)/GM)));
Nathan
@terakilobyte
Apr 07 2015 19:22
make sure you round
Ben McMahon
@benmcmahon100
Apr 07 2015 19:23
ah... yet again have i managed to make the stupidest mistake possible :p
Nathan
@terakilobyte
Apr 07 2015 19:24
nah
I had to make it so that it’d round to avoid any possibility of error with floating point precision
not sure where I said to round in the instructions but it certainly isn’t bolded or anything
Ben McMahon
@benmcmahon100
Apr 07 2015 19:25
where should I round? Each var or the end result?
Nathan
@terakilobyte
Apr 07 2015 19:25
nah round at the beginning
Math.round(2*Math.PI*…)
Ben McMahon
@benmcmahon100
Apr 07 2015 19:26
It it's giving NaN
blob
Brian
@bconner22
Apr 07 2015 19:28
@benmcmahon100 I had that NaN on that one, I remember having to play with Math.round or something in the formula, one item was throwing everything off. Start taking an item at a time out of your formula till you find it
Ben McMahon
@benmcmahon100
Apr 07 2015 19:29
when it console.logs it gives the right answer as an int
altually no it doesn't :p
test 2 tries to get the square root of a minus number
Suzanne Atkinson
@AdventureBear
Apr 07 2015 19:35
@terakilobyte, thanks, I
I'm out having a late lunch, i'll PM you this evening. Any time better?
Nathan
@terakilobyte
Apr 07 2015 19:39
no if I’m around I’ll be available
Ben McMahon
@benmcmahon100
Apr 07 2015 19:40
This message was deleted
This message was deleted
This message was deleted
Ben McMahon
@benmcmahon100
Apr 07 2015 20:26
anyone know why this returns true?
var ret = [1, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
if(ret.indexOf(NaN)>-1){
    return false;
  }
  else{
    return true;
  }
Cristián Berríos
@crisberrios
Apr 07 2015 20:27
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
NaN === NaN
=> false
Ben McMahon
@benmcmahon100
Apr 07 2015 20:29
@crisberrios so i have to make a new function to replace my indexOf?
ThatHighlander
@ItsThatHighlander
Apr 07 2015 20:29
anyone done http://www.freecodecamp.com/bonfires/no-repeats-please ? I keep crashing Chrome tabs :laughing:
Cristián Berríos
@crisberrios
Apr 07 2015 20:30
if you want to find a NaN better use another method (even a for loop) + isNaN
your recursion must be going infinite
Ben McMahon
@benmcmahon100
Apr 07 2015 21:09
I have this solution for the http://www.freecodecamp.com/bonfires/inventory-update bonfire. Is there any easy way to convert the final object back into an array so that it passes the test?
function inventory(arr1, arr2){
  var dat = {};

  var i = 0;
  while(i<arr1.length){
    dat[arr1[i][1]] = arr1[i][0];
    i++;
  }

  i = 0;
  while(i<arr2.length){
    dat[arr2[i][1]] = arr2[i][0];
    i++;
  }

  console.log(dat);

  return(dat);

}

// Example inventory lists
var curInv = [
    [21, 'Bowling Ball'],
    [2, 'Dirty Sock'],
    [1, 'Hair Pin'],
    [5, 'Microphone']
];

var newInv = [
    [2, 'Hair Pin'],
    [3, 'Half-Eaten Apple'],
    [67, 'Bowling Ball'],
    [7, 'Toothpaste']
];

inventory(curInv, newInv);
ThatHighlander
@ItsThatHighlander
Apr 07 2015 21:22
can someone review my code quickly? It passes all the tests, but not sure if it's the most efficient way of doing it... http://pastebin.com/TThAibVD
and suddenly there's no bonfires left :(
Danny Fritz
@dannyfritz
Apr 07 2015 21:27
congrats. haha
@ItsThatHighlander there are some optimizations you can do. If the array you are going to recurse on is going to include a double, don't recurse over it
then at the end, instead of counting ones without doubles, you can count the length of the array
ThatHighlander
@ItsThatHighlander
Apr 07 2015 21:29
@dannyfritz good points, cheers
Danny Fritz
@dannyfritz
Apr 07 2015 21:32
@ItsThatHighlander a really easy optimization you could do is pull the regex out into an outer scope var. if you do inline // i imagine it has to recreate the regex every time.
another one, don't do Array.length in a for loop, cache the length and use that so it doesn't have to access an object member each iteration.
KidLogic
@KidLogic
Apr 07 2015 21:46
I believe this might be a better place to ask this question: Why is it that JQuery is overwriting itself? "I am a paragraph" should pop up after the first div (id = "one") and second div (id = "two"). https://jsfiddle.net/jonplaca/xo4kuzxo/#&togetherjs=t58Hr2xCjK
<!DOCTYPE html>
<html>
    <head>
        <title>Result</title>
        <script type='text/javascript' src='script.js'></script>
    </head>
    <body>
        <div class="container">
            <h2>Greetings</h2>
            <div id="one">Div #1</div>
            <div id="two">Div #2</div>
        </div>   
    </body>
</html>
$(document).ready(function() {
    var $p = $('<p> I am a paragraph </p>');

    $('#one').after($p);
    $('#two').after($p);

});
Danny Fritz
@dannyfritz
Apr 07 2015 21:47
@KidLogic you can't have one DOM Element in 2 places.
so when you attach it twice like that only the last one stays
you'll have to do a clone if you want 2 of them
KidLogic
@KidLogic
Apr 07 2015 21:49
DOM element = '$p'?
Danny Fritz
@dannyfritz
Apr 07 2015 21:59
<p> I am a paragraph </p>
$p in this case if the JQuery collection which contains only that one DOM element.
KidLogic
@KidLogic
Apr 07 2015 22:02
@dannyfritz Thanks for the followup. While I understand that JQuery is running through the process and only appending the variable to the last <div> (with id = two), I don't understand why it would neglect/overwrite div (with id= one)
Danny Fritz
@dannyfritz
Apr 07 2015 22:02
it is actually writing it to both
but because you can only have a DOM element in one spot you only see it on the last one
because you moved it
KidLogic
@KidLogic
Apr 07 2015 22:03
@dannyfritz Ah wait
@dannyfritz I think I understand. JQuery is just "moving" the variable $p around - not necessarily importing/duplicating it
Danny Fritz
@dannyfritz
Apr 07 2015 22:04
yeah, you can duplicate it with $p.clone() if you want it in both places
you'll have 2 DOM elements now
KidLogic
@KidLogic
Apr 07 2015 22:04
@dannyfritz Thank you for this!
Danny Fritz
@dannyfritz
Apr 07 2015 22:05
@KidLogic it is explained on the clone documentation page.
KidLogic
@KidLogic
Apr 07 2015 22:06
@dannyfritz Thanks - I did take a look at the .clone() page but it wasn't clicking at first (still catching up on developer jargon)
thank you for your patience
Danny Fritz
@dannyfritz
Apr 07 2015 22:08
@KidLogic no worries. i'm going through the same process with the CouchDB docs currently.
fortunately the couchdb irc channel is very responsive and helpful. :)