These are chat archives for FreeCodeCamp/Help

6th
Apr 2015
Suzanne Atkinson
@AdventureBear
Apr 06 2015 02:17
so...spinal case... I think this code works but when I put it in a bonfire, the user tests don't run. What's going on?
Tim Pinoy
@waeren
Apr 06 2015 02:29
@AdventureBear All your variables are global. You need to use the var keyword
Suzanne Atkinson
@AdventureBear
Apr 06 2015 02:39
@waeren thanks.
Evgeny Klimenchenko
@Dzheky
Apr 06 2015 04:34
hey guys, can somebody explain the array.filter(); I've read it in MDN, and still can't understand what it does
Cristián Berríos
@crisberrios
Apr 06 2015 04:49
uh, I'd have a hard time explaining it better than MDN
but filter acts as a gate for array items
if they pass the test (function returns true) then the item gets pushed to the target array
if it returns false, it's discarded
http://reactive-extensions.github.io/learnrx/ here you can learn all the array methods by implementing them
:)
Szajbob
@Szajbob
Apr 06 2015 10:34

Hi All! I have a problem with symmetric difference bonfire, I wrote the function, but one of the checks the freecodecamp performs does not look correct. This one:

assert.deepEqual(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'should return an array of unique values');
AssertionError:should return an array of unique values: expected [ 2, 4 ] to deeply equal [ 1, 4, 5 ]

It should return [1, 1, 2, 4], not [1, 4, 5] as I understand the symmetric difference

Bianca Mihai
@biancamihai
Apr 06 2015 10:39
@Szajbob I posted this a few days ago, I think it will help you :) https://www.youtube.com/watch?v=PxffSUQRkG4
so sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) = sym(sym([1, 2, 5], [2, 3, 5]),[3, 4, 5]) = sym([1,3],[3,4,5]) = [1,4,5] :)
Szajbob
@Szajbob
Apr 06 2015 10:41
Thank you for replying- I understand the symmetric difference, but please look once again on the check the freecodecamp performs, it's not the arrays you used in your example. The result [1, 4, 5] is correct for them, but not for the one in their check
Bianca Mihai
@biancamihai
Apr 06 2015 10:46
can you please post the link to that bonfire? :) I can't go back ...
Bianca Mihai
@biancamihai
Apr 06 2015 10:46
i think there was a comment there that if an element is used, don;t use it again ...
Szajbob
@Szajbob
Apr 06 2015 10:47
such comment was in earlier bonfire, when you checked the sum of element is equal to 2nd argument
Bianca Mihai
@biancamihai
Apr 06 2015 10:47
"assert.deepEqual(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'should return an array of unique values');"
Szajbob
@Szajbob
Apr 06 2015 10:48
Yes, and if your function returns something, it will say that this assertion should equal [1, 4, 5]
assert.deepEqual(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'should return an array of unique values');
AssertionError:should return an array of unique values: expected [ 2, 4 ] to deeply equal [ 1, 4, 5 ]
my function returns [2, 4] - which is wrong, but I can work with that. Which baffles me is how this 3 arrays have symmetric difference of [1, 4, 5]
Bianca Mihai
@biancamihai
Apr 06 2015 10:51
for this ex I think sym([1, 1, 2, 5],sym( [2, 2, 3, 5], [3, 4, 5, 5])) = sym([1, 1, 2, 5], [2,4]) = [1,5,4] //because it has to be unique [1,1,4,5] not unique
Szajbob
@Szajbob
Apr 06 2015 10:56
ok, but that doesn't sound like symmetric difference - [1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5] - '1, 1' from the first array is not a part of any other arrays so it should be untouched in this symmetric difference comparison, or maybe I just think I understand the symmetric difference :)
Szajbob
@Szajbob
Apr 06 2015 14:36
I got this! Needed to work on my math to understand, that the comparison in symmetric difference is made between no more than 2 arrays at once, and that repeating elements in an array are counted as one, because the value they represent is important, not the 'physical' presence. Thank you @biancamihai for your assistance! :)
Suzanne Atkinson
@AdventureBear
Apr 06 2015 15:50
Hello everyone! I'm having trouble with Spinal Tap bonfire. I quickly got the tests to pass for a regular spaced sentence and for an underscored sentence, but going from CamelCase to spinal-case is proving to be more irritating. Were people able to do t his as a one-liner or built in function? When I use regex to match capital letters and split the string, I lose the capital letter.
e.g
var reg = /[A-Z]/;
  var camelCaseSplit = str.split(reg);
returns this
[ 'this', 's', 'pinal', 'ap' ]
for this input string: thisIsSpinalTap
I wanted to split the string and insert a space, then use the rest of the function I wrote which works for the other cases.
do i need to use some messy string substitutions to do this?
Suzanne Atkinson
@AdventureBear
Apr 06 2015 16:00
hang on , maybe this is simpler than I thought
Suzanne Atkinson
@AdventureBear
Apr 06 2015 16:12
or not.
Evan Davis
@davisec52
Apr 06 2015 16:22
@AdventureBear Hi. Looking at my notes, I did this with two lines regex and the str.replace method.
Let me clarify. I used regex and two instances of str.replace.
Evan Davis
@davisec52
Apr 06 2015 16:32
@AdventureBear Also, check out the example on switching words in a string to see how it might apply to the spinal case problem: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Catalin Iosif
@ciosifweb
Apr 06 2015 16:57

hello everyone, I'm having a really hard time with the last bonfire:

Return the number of total permutations of the provided string that don't have repeated consecutive letters.

Bianca Mihai
@biancamihai
Apr 06 2015 16:58
@Szajbob you found the solution by your self! :)
Catalin Iosif
@ciosifweb
Apr 06 2015 16:58
I'm trying to come up with the number without actually generating the permutations
but the logic that I came up with still requires that I generate subsets ( for the number of repetitions of each repeated char)
so, if for the case with 3 repeating chars I have to generate subsets of 2 out of 3
for 4 repeating chars I would need to generate subsets of 3 out of four and 2 out of four, and so on
am I complicating things too much?
Catalin Iosif
@ciosifweb
Apr 06 2015 17:08
:)) darn, I realized now that I was only treating the case when all the chars of a certain type are next too each other, so it would have returned a wrong value for more than two repetitions of a char
back to the drawing board
Still, should I keep looking for a way to calculate the value, or just generate the permutations?
Szajbob
@Szajbob
Apr 06 2015 17:19
@biancamihai Yes! I feel so empowered now! :D
@ciosifweb would love to help, but I have not yet reached that bonfire. On the other hand, you should remember, how much explaining to someone your reasoning, by writing or talking helps. This is an advice my coding buddy told me, that everyone that codes should have a small yellow duck sitting next to monitor, just to talk to it, especially when you have a problem :)
Catalin Iosif
@ciosifweb
Apr 06 2015 17:26
@Szajbob yeah, you're absolutely right, after writing it here I saw my flawed thinking really fast
it really helps
Catalin Iosif
@ciosifweb
Apr 06 2015 17:37
I'm just going to generate the permutations and check them as I build them for repeated chars
even with storing the results you can generate about 60k permutations in one second
so it shouldn't be a big deal.
Elliescode
@EllieAdam
Apr 06 2015 18:24
hello! I have question.
Ben McMahon
@benmcmahon100
Apr 06 2015 18:25
Fire away @EllieAdam !
Sudeepto
@Sudeepto
Apr 06 2015 18:52
@AdventureBear Did you solved the CamelCase to Spinal Case conversion ??
Suzanne Atkinson
@AdventureBear
Apr 06 2015 19:04
not yet, took a break for PT
@davisec52, ok thanks good to know it can be done so simply, i'll read up again
Evan Davis
@davisec52
Apr 06 2015 20:08

I'm working on one of the nodeschool challenges, and I
don't really understand why the map method returns ['dog', cat]
instead of [message: "dog", message: "cat"].

item.message returns [message: "dog", message: "cat"], and
it is not clear to me why running it through map gives
us just the values.


function shorter(messages) {
  var x = messages.filter(function(item) {
    return item.message.length < 10;
  });

  return x.map(function(item) {
    return item.message

  });
}
shorter([{message: "dog"}, {message: "cat"}, {message: "bengal tiger"}])
Brian
@bconner22
Apr 06 2015 20:10
@davisec52 because when you're returning item.message, it's returning what is in the 'message' property of those objects
Ben McMahon
@benmcmahon100
Apr 06 2015 20:11
@davisec52 you need return "message: " + item.message to get that
Evan Davis
@davisec52
Apr 06 2015 20:11
@bconner22 Right, but I don't get how it works. It looks redundant.
Ben McMahon
@benmcmahon100
Apr 06 2015 20:11
and return "message: " + item.message.length < 10; for the other line
Brian
@bconner22
Apr 06 2015 20:12
you could just return item, if that's what you're looking for
Evan Davis
@davisec52
Apr 06 2015 20:13
yes, the result that I want is just ["dog", "cat"]. It is not clear to me how map is returning just the values.
Ben McMahon
@benmcmahon100
Apr 06 2015 20:14
@davisec52 try removing the .message part in return item.message.length < 10; and return item.message;
it may make move sense then why it is there
Evan Davis
@davisec52
Apr 06 2015 20:16
@benmcmahon100 Well, the item is just the object in the array and that would not return anything--just []
Ben McMahon
@benmcmahon100
Apr 06 2015 20:17
@davisec52 It should return an object
Evan Davis
@davisec52
Apr 06 2015 20:17
I understand the first instance of item.message, it is just puzzling to me that running it messages through map gives us just the values.

function shorter(messages) {
  var x = messages.filter(function(item) {
    return item.length < 10;
  });
  return x;
}
shorter([{message: "dog"}, {message: "cat"}, {message: "bengal tiger"}])
returns []
Ben McMahon
@benmcmahon100
Apr 06 2015 20:19
sorry try this

function shorter(messages) {
  var x = messages.filter(function(item) {
    return item.message.length < 10;
  });

  return x.map(function(item) {
    return item
  });
}
shorter([{message: "dog"}, {message: "cat"}, {message: "bengal tiger"}])
including message is essential for the length bit
Evan Davis
@davisec52
Apr 06 2015 20:19
That's right--that does return the object
In filter, item.message returns an object, and in map, it returns the object values.
Ben McMahon
@benmcmahon100
Apr 06 2015 20:20
object has no property length
but strings do
so that's the purpose of the .message in the first part
the other message is the because as you saw it's only ment to return the message property of the item object
if that makes any sense
Evan Davis
@davisec52
Apr 06 2015 20:22
isn't message the property/key and "dog" the value?
Ben McMahon
@benmcmahon100
Apr 06 2015 20:23
@davisec52 exactly
Brian
@bconner22
Apr 06 2015 20:25
@davisec52 are you looking for this as an answer? [ { message: 'dog' }, { message: 'cat' } ]
Ben McMahon
@benmcmahon100
Apr 06 2015 20:25
@davisec52 if you ever want to get the number of properties/keys in a object it has to be done like this http://stackoverflow.com/questions/5223/length-of-a-javascript-object-that-is-associative-array
Evan Davis
@davisec52
Apr 06 2015 20:26
@bconner22 The function gives the result I'm seeking, I just did not get how map produced values ["dog", "cat"].
Ben McMahon
@benmcmahon100
Apr 06 2015 20:26
@davisec52 the map doesn't do anything
Evan Davis
@davisec52
Apr 06 2015 20:26
I know what is happening; I don't fully understand how it is happening.
Ben McMahon
@benmcmahon100
Apr 06 2015 20:26
it just returns the result
Evan Davis
@davisec52
Apr 06 2015 20:27
Well, something strips away the objects keys leaving only the values.
Brian
@bconner22
Apr 06 2015 20:29
map doesn't seem to be needed for this problem, if all you're doing is listing everything < length of 10
Ben McMahon
@benmcmahon100
Apr 06 2015 20:30
@davisec52 remove the map and the result doesn't change
it takes each value in the object and hands it straight back
Evan Davis
@davisec52
Apr 06 2015 20:36

function shorter(messages) {
  return messages.filter(function(item) {
    return item.message.length < 10
  });
}
shorter([{message: "dog"}, {message: "cat"}, {message: "bengal tiger"}]) //=> [ { message: 'dog' }, { message: 'cat' } ]
Brian
@bconner22
Apr 06 2015 20:36
there ya go
Evan Davis
@davisec52
Apr 06 2015 20:37
@bconner22 this is moot, but the desired answer is ["dog", "cat"]. The use of map gave that result.
Brian
@bconner22
Apr 06 2015 20:39
ohhhh ok well one way to do that is to make a fresh array and .push your results into that
Ben McMahon
@benmcmahon100
Apr 06 2015 20:40
@davisec52 you can also do
var hold_this = [];
var i = 0;
while(i<messages.length){
hold_this.push(messages[i].message);
i++;
}
return(hold_this);
map does (effectively) that for you
Evan Davis
@davisec52
Apr 06 2015 20:43
Very nice!
Brian
@bconner22
Apr 06 2015 20:44
ok gonna try markdown and then I gotta go but this is how I would do it:
var total=[];
function shorter(messages) {
  messages = messages.filter(function(item) {
    if(item.message.length < 10){
            total.push(item.message);
        }

  });

  return total;


}
shorter([{message: "dog"}, {message: "cat"}, {message: "bengal tiger"}])
Ben McMahon
@benmcmahon100
Apr 06 2015 20:45
very messy! :p
Brian
@bconner22
Apr 06 2015 20:48
now, it my total.push, try total.push(item) vs. total.push(item.message) and see what happens, that should help you understand what is happening. When you call item.message, it returns the value of message instead of message: 'dog' etc. if you call item, it will give you the whole tamale
Evan Davis
@davisec52
Apr 06 2015 20:57
@bconner22 @bconner22 Right. Pushing the object into an array presents just the values. Originally, I thought that should be happening with the filter method, but I see now that it does not. Filter merely identifies the objects of the desired length, and map completes the task returning the items in an array of just the values.
Brian
@bconner22
Apr 06 2015 21:20
Well i dont think it will push just the values if you were to push just item instead of item.message, but i haven't used .filter very much
Suzanne Atkinson
@AdventureBear
Apr 06 2015 22:22
:point_up: April 6 2015 12:32 PM @davisec52 thanks for the link, what i tried was to find a capital letter, and insert a space before the letter. But I can't figure out how to retrieve the matched capital from the regex capture group.
I see that you can capture a group, but I'm not clear on how or where to reuse the group. this is what I tried.
  var reg = /[(A-Z)]/;
  var camelCaseSplit = str.replace(reg, " "+ p1);