24th
Feb 2015
Oliver
Feb 24 2015 00:09 UTC
Can someone explain the logic of this numeric sort function?
//function for numberical sorting
function sortNumber(a,b) {
return a - b;
}
for sorting an array of numbers
Danny Fritz
@dannyfritz
Feb 24 2015 00:36 UTC
is b is larger than a it will return negative
if a is larger than b it will return positive
Matthew Harames
@Harames
Feb 24 2015 01:19 UTC
@crisberrios You still here because my code isn't working for the Title Case a Sentence
No OK then I have to leave
Cristián Berríos
@crisberrios
Feb 24 2015 01:34 UTC
I'm back
you should post the code in question :D
Matthew Harames
@Harames
Feb 24 2015 01:40 UTC
@crisberrios I actually figured it out
I would show you my end result, but bonfire doesn't save your code
@crisberrios Here was my original code though
function titleCase(str) {
str = str.toLowerCase();
var test2 = [];
var test = str.split(" ");
for(var i = 0; i < test.length; i++) {
if(test[i].length === 1) {
test2[i] = test[i].toUpperCase();
} else {
var split = test[i].split("");
var upper = split[1].toUpperCase();
var together = split.join("");
test2[i] = together;
}
}
test2 = test2.join(" ");
return test2;
}

titleCase("I'm a little tea pot");
I believe I changed it to
Cristián Berríos
@crisberrios
Feb 24 2015 01:42 UTC
it wasn't far
Matthew Harames
@Harames
Feb 24 2015 01:42 UTC
function titleCase(str) {
str = str.toLowerCase();
var test2 = [];
var test = str.split(" ");
for(var i = 0; i < test.length; i++) {
if(test[i].length === 1) {
test2[i] = test[i].toUpperCase();
} else {
var split = test[i].split("");
split[0] = split[0].toUpperCase();
var together = split.join("");
test2[i] = together;
}
}
test2 = test2.join(" ");
return test2;
}

titleCase("I'm a little tea pot");
Cristián Berríos
@crisberrios
Feb 24 2015 01:42 UTC
another option without splitting is using regex
if you want to try going down that road
Matthew Harames
@Harames
Feb 24 2015 01:42 UTC
@crisberrios I dislike regex
How do I access arrays in arrays?
Matthew Harames
@Harames
Feb 24 2015 01:53 UTC
Never mind I am leaving
Cristián Berríos
@crisberrios
Feb 24 2015 01:55 UTC
@PKMNinja arr[i][j]
Nathan
@terakilobyte
Feb 24 2015 07:27 UTC
@PKMNinja bonfire saves your code, the end user just can’t see it yet
it will be a part of your profile, every solution you came up with
You can also complete a bonfire multiple times and each solution is saved, in fact
Vince
@Vince33
Feb 24 2015 07:43 UTC
I want to make sure I understand a concept of lastIndexOf and IndexOf when passed to filter()
I think I got it just want to be certain
Cristián Berríos
@crisberrios
Feb 24 2015 07:45 UTC
what's confusing you?
Vince
@Vince33
Feb 24 2015 07:49 UTC
Well if I pass to args to filter, elem and pos, I check that both lastIndexOf and IndexOf are equal to position it is checking that there isn't elem existing at two positions right
positions being indexes
I can get the function to return the value I like but I want to understand it on a deeper level
Cristián Berríos
@crisberrios
Feb 24 2015 07:52 UTC
can you give a code example? indexOf and lastIndexOf can point to the same index inside an array or string
Vince
@Vince33
Feb 24 2015 07:53 UTC
solved it earlier today in a pair session and another camper was looking for help later on and it took me a little bit to reconstruct it when I went to resolve it later
Cristián Berríos
@crisberrios
Feb 24 2015 07:56 UTC
ok I think I get it
var uniqueArr = newArr.filter(function (elem, pos) {
return newArr.lastIndexOf(elem)==pos && newArr.indexOf(elem)==pos;
1st you concatenate 2 arrays
so arr1 = [a,b,c,d] and arr2 = [d,e,f,g]
Vince
@Vince33
Feb 24 2015 07:57 UTC
yes
Cristián Berríos
@crisberrios
Feb 24 2015 07:57 UTC
(note the repeated d)
if an item isn't repeated
then indexOf(a) has to be equal to lastIndexOf(a)
otherwise, the item is in 2 positions within the array, and that would mean that it's in both arrays
Vince
@Vince33
Feb 24 2015 07:58 UTC
right
so pos is index as well
Cristián Berríos
@crisberrios
Feb 24 2015 07:59 UTC
yes
if you use the filter method
Vince
@Vince33
Feb 24 2015 07:59 UTC
yes
Cristián Berríos
@crisberrios
Feb 24 2015 07:59 UTC
it will pass every item to the function indicating the element and the position within the array
Vince
@Vince33
Feb 24 2015 08:00 UTC
right
It just seems to easy
ok I think I am thinking about it the right way then
thanks it was nagging at me
Cristián Berríos
@crisberrios
Feb 24 2015 08:04 UTC
function diff(arr1,arr2) {
return arr2.indexOf(item) >= 0 ;
});
}
well, this returns items found in both
Vince
@Vince33
Feb 24 2015 08:06 UTC
ok phew
Cristián Berríos
@crisberrios
Feb 24 2015 08:06 UTC
but it's kind of similar
Vince
@Vince33
Feb 24 2015 08:07 UTC
I saw quite a few uses of indexOf on stack overflow
sometimes too much info is bad though
trying to assimilate it all at once
I really like the filter method
Cristián Berríos
@crisberrios
Feb 24 2015 08:09 UTC
and then, after graduating from FCC, you'll start to love some libraries https://lodash.com/docs#difference
Nathan
@terakilobyte
Feb 24 2015 08:09 UTC
cough ramda
Vince
@Vince33
Feb 24 2015 08:10 UTC
yeah I took a gander it seems like there is some nice shorthands for this
I though about trying to modify the prototype but might be a bit excessive for bonfire
so with ramda data first right
Nathan
@terakilobyte
Feb 24 2015 08:12 UTC
no!
data last
always
Vince
@Vince33
Feb 24 2015 08:13 UTC
yeah that s what I meant same difference
hehe
wow yeah these lib could make things alot easier
Cristián Berríos
@crisberrios
Feb 24 2015 08:14 UTC
and you can compose them
Vince
@Vince33
Feb 24 2015 08:14 UTC
but I guess this is good for the old interview and actual js knowledge
Cristián Berríos
@crisberrios
Feb 24 2015 08:14 UTC
so... for the problem using LODASH it would be like...
Vince
@Vince33
Feb 24 2015 08:14 UTC
compose them like make your own
?
Cristián Berríos
@crisberrios
Feb 24 2015 08:15 UTC
like passing the result of the function to another function
.funcA(_.funcB(a,b),.funcD(f);
you can mix intersection + difference + flatten
Vince
@Vince33
Feb 24 2015 08:16 UTC
like a chain
Cristián Berríos
@crisberrios
Feb 24 2015 08:16 UTC
hmm not quite
in this case, you use functions as parameters
and build higher order functions
chaining is serializing functions
of course, you can chain and serialize
I mean
compose and hcain
chain
Vince
@Vince33
Feb 24 2015 08:18 UTC
yeah kind of get I have to stop my mind from thinking about recurssive calls
I see its different
this is getting fun !
I sort of appreciate alot of php's functions now
Is there any good reading material on higher order functions
sorry my brain is going in alot of diff directions once
Vince
@Vince33
Feb 24 2015 08:27 UTC
@crisberrios Hey man thanks again! it is like three in the morning I have to get some zzzz
Cristián Berríos
@crisberrios
Feb 24 2015 08:27 UTC
here too
so I think I'm also off for tonight
Vince
@Vince33
Feb 24 2015 08:27 UTC
nice another insomniac
night sir and thanks!
Jordan Wood
@jordanw
Feb 24 2015 08:28 UTC
Dreaming in JS
Vince
@Vince33
Feb 24 2015 08:28 UTC
@jordanw ha
Jordan Wood
@jordanw
Feb 24 2015 08:28 UTC
lol. g'night.
Vince
@Vince33
Feb 24 2015 08:28 UTC
g night
Evan Davis
@davisec52
Feb 24 2015 16:16 UTC
I'm working on the "Where Art Though" Bonfire challenge and having some difficulty. The code sample below
appears to return the correct result for the first input but returns a blank array for second (commented out) input sample. I'd appreciate a little direction on this on. A bit lost on it.

function where(collection, source) {
var arr = [];
var obj = {};
for(var i in source) {
var x = source[i];
for(var j = 0; j < collection.length; j++) {
var y = collection[j];
for(var p in y) {
for(var q in x) {
if(y.hasOwnProperty(p) === x.hasOwnProperty(q)) {
obj[p] = y[p];
arr = [obj];

}
}
}
}
}

//return obj;
return arr;
}
where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });

//where([{ 'a': 1 }, { 'a': 1 }, { 'a': 1, 'b': 2 }], { 'a': 1 });
Cristián Berríos
@crisberrios
Feb 24 2015 16:20 UTC
hey
Evan Davis
@davisec52
Feb 24 2015 16:20 UTC
Hi
Cristián Berríos
@crisberrios
Feb 24 2015 16:20 UTC
for(var i in source) { var x = source[i];
remember that variables don't have block scope
you must declare them outside the loop
well, I stopped checking at the 3rd loop or so
Evan Davis
@davisec52
Feb 24 2015 16:22 UTC
so I should take all of the variables in the loops and declare them at the top?
Cristián Berríos
@crisberrios
Feb 24 2015 16:22 UTC
it's really a bad practice to say for(a in b) { for (c in b) { for (d in c) { ...... }}}}
also, since you aren't being expressive on what each variable means
at least do for (person in array) { for (property in person) {}
Evan Davis
@davisec52
Feb 24 2015 16:23 UTC
That makes intuitive sense, but I'm just "feeling around" trying to get an idea of how to solve this.
Cristián Berríos
@crisberrios
Feb 24 2015 16:24 UTC
to see a general idea on what you're trying to do
Evan Davis
@davisec52
Feb 24 2015 16:24 UTC
Here is my understanding of what I need to do: compare properties of an object with the properties of objects within in an array.
Cristián Berríos
@crisberrios
Feb 24 2015 16:25 UTC
yeah, that's right
Evan Davis
@davisec52
Feb 24 2015 16:25 UTC
So all of those loops were me trying to get at the properties and values to compare them
Cristián Berríos
@crisberrios
Feb 24 2015 16:26 UTC
well, without checking further
Evan Davis
@davisec52
Feb 24 2015 16:26 UTC
Sort of like trying to eat noodles with your hands tied behind your back'
Cristián Berríos
@crisberrios
Feb 24 2015 16:26 UTC
I could say that it's probable that you're just checking values
Evan Davis
@davisec52
Feb 24 2015 16:26 UTC
perhaps
Cristián Berríos
@crisberrios
Feb 24 2015 16:26 UTC
and not property names + values
since I don't see any Object.keys around
Evan Davis
@davisec52
Feb 24 2015 16:27 UTC
ok
Cristián Berríos
@crisberrios
Feb 24 2015 16:30 UTC
try to use Object.keys to get property names and refactor the code into "self documenting" variables, so it's easier to understand the intent of the code
and that way it's easier to debug from an outside perspective
Evan Davis
@davisec52
Feb 24 2015 16:31 UTC
What are "self documenting" variables?
Cristián Berríos
@crisberrios
Feb 24 2015 16:32 UTC
like, if you want to store a person object, you declare personObj =
or a persons array... personsArr =
for (person in personsArr)
weell
well
Evan Davis
@davisec52
Feb 24 2015 16:33 UTC
Ok, thank you.
Cristián Berríos
@crisberrios
Feb 24 2015 16:33 UTC
better if you omit Obj and Arr depending on the situation
Evan Davis
@davisec52
Feb 24 2015 16:34 UTC
Right. Ok, I'll try some solutions using Object.keys.
Germaine Pannell
@germainep
Feb 24 2015 16:47 UTC
Hello guys
I was wondering if I could get some help with the Spinal Tap Case bonfire
Oliver
Feb 24 2015 16:49 UTC
Can you post the description?
Germaine Pannell
@germainep
Feb 24 2015 16:50 UTC
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
Serhii Borovskyi
@serhiicss
Feb 24 2015 16:50 UTC
var cashRegister = {
total: 0,
this.total += itemCost;
};

scan: function (item) {
switch (item) {
case "eggs":
break;

case "milk":
break;

case "magazine":
break;

case "chocolate":
break;

}
return true;
}
};
Can anyone see a syntax problem here?
I get an unexpected token callback on this :(
Cristián Berríos
@crisberrios
Feb 24 2015 16:53 UTC
@germainep what have you tried on that bonfire?
@serhiicss let's see
Germaine Pannell
@germainep
Feb 24 2015 16:53 UTC
@crisberrios we have gotten all the test cases except the camel case one
Cristián Berríos
@crisberrios
Feb 24 2015 16:54 UTC
add: function(itemCost) { this.total += itemCost; };
replace semicolon by a comma
@germainep can you paste the code?
Germaine Pannell
@germainep
Feb 24 2015 16:55 UTC
@crisberrios we can't figure out how to separate the words in camel case
yes

function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var arr= [];
var newStr;
newStr = str.replace(/[\W_]/g, '-').toLowerCase();

return newStr;
}

spinalCase('This Is Spinal Tap');

Cristián Berríos
@crisberrios
Feb 24 2015 16:56 UTC
ticks on their own line before and after
Germaine Pannell
@germainep
Feb 24 2015 16:57 UTC
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var arr= [];
var newStr;
newStr = str.replace(/[\W_]/g, '-').toLowerCase();

return newStr;
}

spinalCase('This Is Spinal Tap');
Cristián Berríos
@crisberrios
Feb 24 2015 16:57 UTC
and what is the case that's failing?
Germaine Pannell
@germainep
Feb 24 2015 16:57 UTC
camelcase
Cristián Berríos
@crisberrios
Feb 24 2015 16:57 UTC
I mean, the test input
Germaine Pannell
@germainep
Feb 24 2015 16:57 UTC
thisIsSpinalTap
Cristián Berríos
@crisberrios
Feb 24 2015 16:58 UTC
well, in that string, what's the word boundary?
how would you define the point where you want to insert a dash?
Germaine Pannell
@germainep
Feb 24 2015 16:59 UTC
any capital letter
of /[A-Z]/
Cristián Berríos
@crisberrios
Feb 24 2015 17:00 UTC
no
don't think in regex
just natural language
where should the dashes go?
Germaine Pannell
@germainep
Feb 24 2015 17:00 UTC
after each word
Cristián Berríos
@crisberrios
Feb 24 2015 17:00 UTC
(assuming the computer doesn't know what a word is)
well... it should be between a lowercase letter followed by an uppercase letter
Germaine Pannell
@germainep
Feb 24 2015 17:01 UTC
between a lower case character followed by an uppercase character
Cristián Berríos
@crisberrios
Feb 24 2015 17:01 UTC
and you can express that in regex
/([a-z]){1}([A-Z]{1})/
Serhii Borovskyi
@serhiicss
Feb 24 2015 17:02 UTC
@crisberrios Thanks!
Germaine Pannell
@germainep
Feb 24 2015 17:02 UTC
can you explain {1}
Cristián Berríos
@crisberrios
Feb 24 2015 17:03 UTC
and using round parentheses you can save the captured characters and insert the dash in between
like
str.replace(/([a-z]{1})([A-Z]{1})/g, function($1,$2) { return $1 + '-' +$2) });
I might be wrong on the syntax
Oliver
Feb 24 2015 17:07 UTC
clearly I need to work on my regex
Cristián Berríos
@crisberrios
Feb 24 2015 17:09 UTC
"thisIsSpinalTap".replace(/([a-z]{1})([A-Z]{1})/g, function(match) {
return match[0].toLowerCase() + '-' + match[1].toLowerCase();
});
Cristián Berríos
@crisberrios
Feb 24 2015 17:16 UTC
I think the 1st form should work, but I have also to get better at using capture groups
Ashley Drake
@aldraco
Feb 24 2015 17:37 UTC
thanks @crisberrios we got it figured out now!
(@germainep and i were pairing on it)
Cristián Berríos
@crisberrios
Feb 24 2015 17:37 UTC
good
Evan Davis
@davisec52
Feb 24 2015 17:40 UTC
@crisberrios I am really lost on the Bonfire "Where Art Thou" challenge. The code sample below does not do anything, though it is an attempt to compare the properties of source (an object) with collection (an array of objects). What is it that the problem wants us to understand in order to solve it?

function where(collection, source) {
var arr = [];
for(var i = 0; i < collection.length; i++) {
var colKeys = Object.keys(collection[i]);
for(var j in source) {
var sourceKeys = Object.keys(source);
if(sourceKeys === colKeys) {
arr = colKeys;

}
}

}

return arr;
}
where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });
Cristián Berríos
@crisberrios
Feb 24 2015 17:40 UTC
well, 1st rule: never define a variable inside a loop
Evan Davis
@davisec52
Feb 24 2015 17:41 UTC
I guess I don't understand. How can you set up a for-loop without defining the variable.
sorry,
right, var sourceKeys
but that is not actually the main problem, though, is it?
Cristián Berríos
@crisberrios
Feb 24 2015 17:42 UTC
function where(collection, source) {
var arr = [];
var i = 0;
var colKeys;
var sourceKeys;
for(i = 0; i < collection.length; i++) {
colKeys = Object.keys(collection[i]);
for(var j in source) {
sourceKeys = Object.keys(source);
if(sourceKeys === colKeys) {
arr = colKeys;
}
}

}

return arr;
}
you can combine declarations in 1 line, but 1st is declaring variables
then...
ok... you store the object keys in colkeys
that's ok
and then the 2nd loop is not right
Evan Davis
@davisec52
Feb 24 2015 17:44 UTC
ok
Cristián Berríos
@crisberrios
Feb 24 2015 17:44 UTC
since you again are passing only values to the j variable
function where(collection, source) {
var arr = [];
var i = 0;
var colKeys;
var  sourceKeys = Object.keys(source);
for(i = 0; i < collection.length; i++) {
colKeys = Object.keys(collection[i]);
for(var key in sourceKeys) {
//still have to compare
}
}

}

return arr;
}
Evan Davis
@davisec52
Feb 24 2015 17:45 UTC
I tried making colKeys = source, but that did not work.
but I thought Object.keys(source) would give the key (last).
Cristián Berríos
@crisberrios
Feb 24 2015 17:46 UTC
see if that makes more sense
Ashley Drake
@aldraco
Feb 24 2015 17:47 UTC
what is the most acceptable way to get involved when you're new to the project - go ahead and fix something, and make a pull request? or ask first? I'm sure there are times when someone would rather assign a problem to someone who knows more about it.
Cristián Berríos
@crisberrios
Feb 24 2015 17:48 UTC
check the issues
Evan Davis
@davisec52
Feb 24 2015 17:48 UTC
@crisberrios I'll need a moment . . .
Ashley Drake
@aldraco
Feb 24 2015 17:48 UTC
not sure about how the personal dynamics tend to work
Cristián Berríos
@crisberrios
Feb 24 2015 17:48 UTC
and try to get an issue you can fix assigned to you
Ashley Drake
@aldraco
Feb 24 2015 17:49 UTC
@crisberrios is just saying "I'm willing to help" what you mean?
Cristián Berríos
@crisberrios
Feb 24 2015 17:49 UTC
or, if there is a bug, submit an issue and better yet submit a possible solution using a pull request
Ashley Drake
@aldraco
Feb 24 2015 17:50 UTC
ok
thanks
:)
Evan Davis
@davisec52
Feb 24 2015 17:54 UTC
@crisberrios What are the training points this problem is trying to get us to practice? I am just lost. Perhaps if I were sure of the training points, I could study those and arrive at a solution.
Germaine Pannell
@germainep
Feb 24 2015 17:56 UTC
@crisberrios thanks very much
Cristián Berríos
@crisberrios
Feb 24 2015 17:58 UTC
@davisec52 I think core key elements are
• pointing to an element of an array
• pointing to an object property name
• pointing to an object property value.
• filter values of a collection
also, more specific:
• learn to use the information returned by Object.keys(obj)
• learn to get a value of a specific key
• learn to get the value of the same key in a different object
Evan Davis
@davisec52
Feb 24 2015 18:01 UTC
@crisberrios Great. For the second and third specific points, could you recommend links to material that would help me understand how to accomplish those objectives?
Cristián Berríos
@crisberrios
Feb 24 2015 18:03 UTC
any general JS book will do. Check the Objects chapter in Eloquent JS, Professional JS for web developers (non-free), JS: The Good parts (non-free) or JS succintly.
for a better way of dealing with loops you could also check the arrays chapter
in special the .filter method for this problem
Evan Davis
@davisec52
Feb 24 2015 18:06 UTC
So, basically, to understand this problem requires reading 1500 pages of javascript textbooks
I know Object,keys and filters are covered on the MDN pages.
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:08 UTC
Nah, those books contain waaaaaaaay more than just those topics :)
Evan Davis
@davisec52
Feb 24 2015 18:08 UTC
Right, I know it was not cris's intention to recommend reading a library
Cristián Berríos
@crisberrios
Feb 24 2015 18:09 UTC
you can also try mdn.io :P
and I said any of those
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:09 UTC
If you read one of them (don't start with The Good Parts though, keep it for your second or third book on JS) you'll learn loads, then experiment and solve problems (I recommend supplementing the bonfires with lots of codewars) until it becomes second nature
Evan Davis
@davisec52
Feb 24 2015 18:10 UTC
I have tried Eloquent JS and found it too difficult.
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:10 UTC
and @crisberrios - I know it shouldn't bother me but arghh it hurts me so to not see a space after for or if!! ;-)
Evan Davis
@davisec52
Feb 24 2015 18:11 UTC
This message was deleted
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:13 UTC
I haven't read Eloquent JS myself but I've seen it come very highly recommended. I read Professional JS for Web Developers as my intro book, it's not ideal but I thought it was great, though sometimes maybe a bit too exhaustive and now maybe a little outdated
Evan Davis
@davisec52
Feb 24 2015 18:14 UTC
Does Professional JS require a background in programming?
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:16 UTC
The author assumes you have some familiarity with programming, but I personally thought it worked as an intro book. sometimes you have to do some googling but it's great if you prefer a book that goes into more depth than you need. it's NOT a very practical book though, there are no exercises like in Eloquent JS unfortunately
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:16 UTC
I don't feel Eloquent JavaScript assumes a background in programming... I'm still in the early chapters, but I feel it's fairly clear.
Sometimes I find myself re-reading stuff, but that's OK.
The book actually tells you to do that.
Cristián Berríos
@crisberrios
Feb 24 2015 18:16 UTC
@wdgreg I have to revise the coding styles... I've always used for(... since my days in C
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:17 UTC
@crisberrios JavaScript has a completely different approach from other more "traditional" languages... or so I've been hearing.
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:17 UTC
@crisberrios it doesn't really matter, it's just that I've read and seen too much of Crockford not to have the lack of spaces affect me ;-)
Cristián Berríos
@crisberrios
Feb 24 2015 18:17 UTC
I still have to go through some of his books
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:19 UTC
what really grinds my gears is that comma-first variable declaration style. grrrrrr
or people who think it's fun to omit semicolons when the engine puts them automatically (damn you, raganwald)
Cristián Berríos
@crisberrios
Feb 24 2015 18:19 UTC
like var a = 0
,b = something?
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:20 UTC
yeah, that's comma first. I just really don't like it, it looks so alien to me
even though it's perfectly valid
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:21 UTC
is that comma first declaration JS?
That looks really weird.
Cristián Berríos
@crisberrios
Feb 24 2015 18:21 UTC
sure, same as var a,b,c
but you declare var a
,b
,c
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:21 UTC
To me that seems like an easy way to get lost... I dunno.
I like keeping things grouped that are grouped. \
Cristián Berríos
@crisberrios
Feb 24 2015 18:22 UTC
well, brb
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:23 UTC
yeah, I read an article the other day and IIRC it said about 25% of JS programmers use comma-first
I really like the AirBnB style guide
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:51 UTC
that looks interesting...
Cristián Berríos
@crisberrios
Feb 24 2015 18:55 UTC
https://github.com/airbnb/javascript/issues/233#issuecomment-72163361 I think I'll add the missing rules to my .jshintrc
Ashley Drake
@aldraco
Feb 24 2015 19:38 UTC
ok ... css question. this is driving me nuts. 
<link rel="stylesheet" href="/public/stylesheets/bootstrap-flatly.css" type="text/css">
should go to the root folder of my project and find the public folder and go down from there, right?
i'm using ejs if that makes any difference
Angelica Ferdinand
@ESOComputing
Feb 24 2015 19:42 UTC
If public is the top-level directory that should work.
The preceding / means the root level.
Nathan
@terakilobyte
Feb 24 2015 19:43 UTC
@aldraco is it an express app?
Ashley Drake
@aldraco
Feb 24 2015 19:43 UTC
it is an express app
Nathan
@terakilobyte
Feb 24 2015 19:43 UTC
if your static directory is set to public
then don’t include /public
Ashley Drake
@aldraco
Feb 24 2015 19:44 UTC
ahh... because all static assets are being routed through there anyway?
Nathan
@terakilobyte
Feb 24 2015 19:44 UTC
^
so try '/stylesheets/bootstrap-flatlly.css'
Ashley Drake
@aldraco
Feb 24 2015 19:45 UTC
worked perfectly.
Nathan
@terakilobyte
Feb 24 2015 19:45 UTC
:)
Ashley Drake
@aldraco
Feb 24 2015 19:45 UTC
thanks!
Nathan
@terakilobyte
Feb 24 2015 19:45 UTC
enjoy!
Germaine Pannell
@germainep
Feb 24 2015 20:35 UTC
has anyone attempted a recursive function in bonfires
Cristián Berríos
@crisberrios
Feb 24 2015 20:36 UTC
I think I've done some recursion
but don't rely on it too much for deep recursion
Germaine Pannell
@germainep
Feb 24 2015 20:37 UTC
I keep crashing it trying to do a recursive fibonacci number just wondering if it was just me
Cristián Berríos
@crisberrios
Feb 24 2015 20:38 UTC
maybe the function itself is crashing
Germaine Pannell
@germainep
Feb 24 2015 20:40 UTC
i think so i'll just d it another way
Nathan
@terakilobyte
Feb 24 2015 20:44 UTC
recursion is not the best solution to fibonaccis imo
as the typical fibonacci recursive method forces recalculation of the right side
that and js doesn’t properly support tail recursion yet
Cristián Berríos
@crisberrios
Feb 24 2015 20:45 UTC
It can be solved using the memoization approach
in the case you want to calculate the nth fibonacci number
Nathan
@terakilobyte
Feb 24 2015 20:46 UTC
^
a while loop that is workign off of known values in an array, and pushign new values to the end of that array to sustain itself is incredibly efficient
and while bonfire won’t actually cache (it doesn’t need to, js is stupid fast for the sizes of inputs we’re giving), in the real world it would cache to a data store somewhere
Cristián Berríos
@crisberrios
Feb 24 2015 20:53 UTC

var fibonacci = function () {
var memo = [0, 1];
var fib = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
Memoization | 45
result = fib(n - 1) + fib(n - 2);
memo[n] = result;
}
return result;
};
return fib;
}();
stole this from js: The Good Parts
Nathan
@terakilobyte
Feb 24 2015 20:55 UTC
I still don’t like that method as it has a recursive call
Cristián Berríos
@crisberrios
Feb 24 2015 20:55 UTC
and this general version
var memoizer = function (memo, fundamental) {
var shell = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = fundamental(shell, n);
memo[n] = result;
}
return result;
};
return shell;
};

//ejemplo
var fibonacci = memoizer([0, 1], function (shell, n) {
return shell(n - 1) + shell(n - 2);
});
//ejemplo 2
var factorial = memoizer([1, 1], function (shell, n) {
return n * shell(n - 1);
});
Oliver
Feb 24 2015 20:57 UTC
Convert the characters "&", "<", ">", '"', and "'", in a string to their corresponding HTML entities. - BONFIRE - What should 'Dolce & Gabbana' return?
'Dolce &#38; Gabbana'?
Nathan
@terakilobyte
Feb 24 2015 20:58 UTC
// fibs is our data store for now
var fibs = [0,1,1];
function findNthFib(n) {
while (fibs.length < n) {
fibs.push(fibs[fibs.length -1 + fibs[fibs.length - 2]);
}
return fibs[n];
}
Cristián Berríos
@crisberrios
Feb 24 2015 20:58 UTC
use entity names
yeah, much faster and also doesn't use recursion
Nathan
@terakilobyte
Feb 24 2015 21:01 UTC
not sure where I saw it, but it’s my goto for really any series
fibs can be cached, so any repeat calls will fail the condition in the while loop if that number already exists
it can be modified to calculate values as well
Oliver
Feb 24 2015 21:02 UTC
@crisberrios Thanks - I was using .charCodeAt + entity numbers
Nathan
@terakilobyte
Feb 24 2015 21:02 UTC
I made the bonfire fibonacci challenge off a modification to that
Oliver
Feb 24 2015 21:25 UTC
I'm curious how others solved this bonfire - Convert the characters "&", "<", ">", '"', and "'", in a string to their corresponding HTML entities.
function convert(str) {

// replace ', ", <, >, & with equivalent HTML values
var newString = "";

// table of values for HTML replacement
var escapeHtml = {
34 : '&quot;',
38 : '&amp;',
39 : '&apos;',
60 : '&lt;',
62 : '&gt;'};

newString = str.replace(/[<>'"\&]/g, function(c) {

//send back the value from escapeHtml table
return escapeHtml[c.charCodeAt(0)];

});
return newString;
}

convert('Dolce & Gabbana');
I would be nice if after you solved a bonfire you could look at other people's solutions like at code wars
Nathan
@terakilobyte
Feb 24 2015 21:27 UTC
you will be able to shortly
Ashley Drake
@aldraco
Feb 24 2015 21:31 UTC
@Dadsaster i did that one similarly
just without the char codes, nice touch
Oliver
Feb 24 2015 21:32 UTC
Thanks
Nathan
@terakilobyte
Feb 24 2015 21:39 UTC
I like it, though you can clean it up by not using the character codes and instead using the symbol itself
Vince
@Vince33
Feb 24 2015 21:43 UTC
@terakilobyte Are our solutions being stored now?
Nathan
@terakilobyte
Feb 24 2015 21:43 UTC
yes
Vince
@Vince33
Feb 24 2015 21:44 UTC
cool so when you add functionality I will be able to see stuff I have already done
@terakilobyte I know you guys were mentioning it earir but didn't get a chance to see the thread of conv.
Nathan
@terakilobyte
Feb 24 2015 21:48 UTC
function convert(str) {

// replace ', ", <, >, & with equivalent HTML values
var newString = "";

// table of values for HTML replacement

newString = str.replace(/[<>'"\&]/g, function(c) {

//send back the value from escapeHtml table
return escape(c);

});
return newString;
}

function escape(html) {
var escapeHtml = {
'\"' : function() {
return '&quot;';
},
'&'  : function() {
return '&amp;';
},
'\'' : function() {
return '&apos;';
},
'<'  : function() {
return '&lt;';
},
'>'  : function() {
return '&gt;';
}
};
return escapeHtml[html]() || html;
}

convert('Dolce & Gabbana’);
Germaine Pannell
@germainep
Feb 24 2015 21:54 UTC
hi, guys I'm working on the Sum All Odd fionacci numbers bonfire and every time I create the fibonacci function it crashes the browser.
Nathan
@terakilobyte
Feb 24 2015 21:56 UTC
the sandbox only provides so much protection
turning it up all the way would keep us from having a lot of nice things
post wha tyou have rq
I’ll see if I can point out any obvious errors
Germaine Pannell
@germainep
Feb 24 2015 21:57 UTC
function sumFibs(num) {
var fib = [];
for(var i=0; i<num; i++){
fib.push((i<2) ? i : fib[i-1] + fib[i-2]);
}
return fib;
}`
this is what I have so far and I want to make sure I get the correct sequence then I can filter the odd numbers and get the correct sum
Nathan
@terakilobyte
Feb 24 2015 22:01 UTC
I have to run to work so someone else will have to take over, but iterating sequentially is most definitely not the answer
also, if i = 5
well, the logic in the for loop is ok, but you’re taking forever to get there
one of the arguments we pass in is pretty big
Germaine Pannell
@germainep
Feb 24 2015 22:10 UTC
@terakilobyte I dont know any other way to get a Fibonacci sequence i guess back to the grind stone
Nathan
@terakilobyte
Feb 24 2015 22:23 UTC
if you scroll up I posted a method earlier
Ashley Drake
@aldraco
Feb 24 2015 22:24 UTC
sum of all primes is a doozy
Nathan
@terakilobyte
Feb 24 2015 22:25 UTC
I’ve been working on higher level bonfires in my spare time
I’ll have another 20 or so to add to level 3 and 4
Ashley Drake
@aldraco
Feb 24 2015 22:26 UTC
the fire keeps growing ... :fire_engine:
Vince
@Vince33
Feb 24 2015 22:26 UTC
@terakilobyte killing me here
Nathan
@terakilobyte
Feb 24 2015 22:26 UTC
you all have a good day, off to work
Ashley Drake
@aldraco
Feb 24 2015 22:28 UTC
take care
Vince
@Vince33
Feb 24 2015 22:52 UTC
@germainep any luck?
Germaine Pannell
@germainep
Feb 24 2015 22:56 UTC
@Vince33 not yet I'm taking a break to clear my mind
Vince
@Vince33
Feb 24 2015 22:56 UTC
@germainep good Idea I am on the same one
Germaine Pannell
@germainep
Feb 24 2015 22:58 UTC
@Vince33 give me about 20 mins and we can pair if you want
Vince
@Vince33
Feb 24 2015 22:58 UTC
send me a pm I should be around
I am running out real fast but should be back
Germaine Pannell
@germainep
Feb 24 2015 22:59 UTC
ok