These are chat archives for FreeCodeCamp/HelpJavaScript

23rd
Jul 2017
Stuhl
@Stuhl
Jul 23 2017 00:04
@Hijerboa No var is not global scoped, they also have function scope if they get defined in one
they difference between let and var is that let is block scoped which means for example if you define one in a if condition and do for example {let a = 1} and want to print it out with console.log outside of the block, you will get an error
var on the other hand gets hoisted and will be global in that case
Sofus Heggemsens
@Sofeo
Jul 23 2017 01:12
hey is there a way to put all arguments in to the filter function?
function destroyer(arr) {
  // Remove all the values
  var arg1 = arguments[1];
  var arg2 = arguments[2];

  var newArr = arr.filter(function(val) {
    return val !== arg1;
  });
  return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
korzo
@korzo
Jul 23 2017 01:14
@Sofeo You have to slice arguments to get all but first element of arguments
@Sofeo because you don't know hove many arguments function has
Sofus Heggemsens
@Sofeo
Jul 23 2017 01:16
@korzo can you exsplain it a little more?
Rob Meador
@RobMeador
Jul 23 2017 01:17
Hey folks. Can someone explain to my why this 'sortNumber' function works? I found this solution for sorting numbers in an array. It works, but I can't seem to figure out why/how it works.
var numList = [2, 45, 51, 4, 1, 10, 38, 30, 3, 5, 7, 78];

function sortNumber(a, b) {
    return a - b;
}

console.log(numList.sort(sortNumber)); 
// [1, 2, 3, 4, 5, 7, 10, 30, 38, 45, 51, 78]

console.log(numList.sort()); // without sortNumber function...
// [1, 10, 2, 3, 30, 38, 4, 45, 5, 51, 7, 78]
korzo
@korzo
Jul 23 2017 01:17
@Sofeo 1. argument is source array and then you have x arguments to filter
@Sofeo Therefore you have to filter rest of arguments using slice
@Sofeo var needles = Array.prototype.slice.call(arguments, 1);
Jake
@JakeDVirus
Jul 23 2017 01:18
@Sofeo your code seems inefficient. if you are given different arguments then you need to rewrite the code everytime.
do something that your one piece of code will work for every argument no matter what
korzo
@korzo
Jul 23 2017 01:19
@Sofeo and the filter arr and remove all elements that are in needles
Dovydas Stirpeika
@Giveback007
Jul 23 2017 01:19
hey peeps
if I use redux will my app be faster or slower?
Stanley Su
@stanley-su
Jul 23 2017 01:21

@RobMeador According to MDN:

The default sort order is according to string Unicode code points.

var scores = [1, 10, 21, 2]; 
scores.sort(); // [1, 10, 2, 21]
// Note that 10 comes before 2,
// because '10' comes before '2' in Unicode code point order.
Ryan Williams
@Ryanwfile
Jul 23 2017 01:22
I am trying to finish the game of life project and currently I have everything working mostly except my function that is supposed to re-render the board repeatedly only happens once. Any help getting my board to continuously re-render would be very appreciated. https://codepen.io/Ryanwfile/pen/qXWxYY
Rob Meador
@RobMeador
Jul 23 2017 01:24
@stanley-su Right, I understand that by default it's sorting by Unicode. In order to try to understand why the sortNumber function seems to solve this, I plugged this code into the Javascript visualizer at www.pythontutor.com. This just confused me further. It seems 'a' and 'b' are pulled randomly from the array, subtracted and then I'm not sure how sort() is interpreting these return values to actually sort the list.
Stanley Su
@stanley-su
Jul 23 2017 01:26

@RobMeador I’m not entirely sure how it works, I think it might use bubblesort. From MDN:

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:
If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

@RobMeador So in

let arr = [2, 45, 51, 4, 1, 10, 38, 30, 3, 5, 7, 78];
arr.sort(function(a, b) {
  return a - b;
});

On the first iteration, a is 2, b is 45.
The function passed to sort returns a negative number. Quote MDN again:

If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

Rob Meador
@RobMeador
Jul 23 2017 01:30
@stanley-su When I ran it through the Javascript visualizer, 'a' and 'b' seemed to have no logically flow. Maybe it's just that tool is inaccurate? I'll try running it through the debugger and see what happens.
Sofus Heggemsens
@Sofeo
Jul 23 2017 01:31
@korzo how do i get the numbers from needles? cause needles is a array now
korzo
@korzo
Jul 23 2017 01:32
@Sofeo You don't have to. Use filter on arr and inside function test, if element provided by filter is in needles
Scott Rhoades
@srhoades
Jul 23 2017 01:33
can anyone give me a hand with a bug in my tic-tac-toe project?
Rob Meador
@RobMeador
Jul 23 2017 01:34
@stanley-su I just ran it through the debugger and it's kicking out the same order of 'a' and 'b' values that seem to be randomly picked from the array (which I doubt is the case - I'm just misunderstanding something). Maybe I'm just dwelling on what's going on "under the hood" too much.
Stanley Su
@stanley-su
Jul 23 2017 01:36

@RobMeador Yeah I’m not entirely sure. I just read this on StackOverflow though:

The JavaScript interpreter has some kind of sort algorithm implementation built into it. It calls the comparison function some number of times during the sorting operation. The number of times the comparison function gets called depends on the particular algorithm, the data to be sorted, and the order it is in prior to the sort.
Some sort algorithms perform poorly on already-sorted lists because it causes them to make far more comparisons than in the typical case. Others cope well with pre-sorted lists, but have other cases where they can be "tricked" into performing poorly.
There are many sorting algorithms in common use because no single algorithm is perfect for all purposes. The two most often used for generic sorting are Quicksort and merge sort. Quicksort is often the faster of the two, but merge sort has some nice properties that can make it a better overall choice. Merge sort is stable, while Quicksort is not. Both algorithms are parallelizable, but the way merge sort works makes a parallel implementation more efficient, all else being equal.
Your particular JavaScript interpreter may use one of those algorithms or something else entirely. The ECMAScript standard does not specify which algorithm a conforming implementation must use. It even explicitly disavows the need for stability.

Sofus Heggemsens
@Sofeo
Jul 23 2017 01:40
@korzo it worked!! thanks im not really sure what i did but im gonna try to understand when i wake upp. but anyways thank you so mutch for the help :D
CamperBot
@camperbot
Jul 23 2017 01:40
sofeo sends brownie points to @korzo :sparkles: :thumbsup: :sparkles:
:cookie: 287 | @korzo |http://www.freecodecamp.com/korzo
Rob Meador
@RobMeador
Jul 23 2017 01:46
@stanley-su Yeah, seems complicated. Thanks for the help!
CamperBot
@camperbot
Jul 23 2017 01:46
robmeador sends brownie points to @stanley-su :sparkles: :thumbsup: :sparkles:
:cookie: 69 | @stanley-su |http://www.freecodecamp.com/stanley-su
Huỳnh Trần Khanh
@khanh2003
Jul 23 2017 01:49

@RobMeador

I just ran it through the debugger and it's kicking out the same order of 'a' and 'b' values that seem to be randomly picked from the array

It is not random.
For small1^1 arrays, your browser uses merge sort algorithm.
For larger1^1 arrays, your browser uses quick sort.

1^1: depend on your browser.

Both algorithms need to compare two elements in array by calling your function.

Scott Rhoades
@srhoades
Jul 23 2017 01:51
https://codepen.io/srhoades/pen/gRyyME?editors=0010 Can anyone help me with my tic-tac-toe project, I can't figure out why the first computers move does not display until the 2nd player move.
Stanley Su
@stanley-su
Jul 23 2017 02:04

@srhoades It’s at line 252, in:

$(".tile").on("click", function playersTurn() {
        validatePlay(this);
        if (playValid) {
            console.log("play valid");
            $(this).removeClass("free");
            $(this).addClass("played");
            $(this).addClass("player");
            checkWin();
            computerPlay();
        } else {
            $(".status").html(
                "That square has already been played. Please choose another square"
            );
        }
    });

After the player clicks on a square, the computer plays.

@srhoades wait i’m wrong let me try to find out
Scott Rhoades
@srhoades
Jul 23 2017 02:06
Thanks @stanley-su
CamperBot
@camperbot
Jul 23 2017 02:06
srhoades sends brownie points to @stanley-su :sparkles: :thumbsup: :sparkles:
:cookie: 70 | @stanley-su |http://www.freecodecamp.com/stanley-su
korzo
@korzo
Jul 23 2017 02:09
@srhoades At the first click, in computerRandomPlay(), playValid id false
Scott Rhoades
@srhoades
Jul 23 2017 02:16
thanks @korzo
CamperBot
@camperbot
Jul 23 2017 02:16
srhoades sends brownie points to @korzo :sparkles: :thumbsup: :sparkles:
:cookie: 290 | @korzo |http://www.freecodecamp.com/korzo
korzo
@korzo
Jul 23 2017 02:17
@srhoades var randomSquare = $("#square" + randomNumber);
you don't have #square
@srhoades change it to #cell and it will work
Scott Rhoades
@srhoades
Jul 23 2017 02:29
thanks again
Joseph
@revisualize
Jul 23 2017 02:43
Hey.
kumquatfelafel
@kumquatfelafel
Jul 23 2017 02:44
hi
weifo
@weifo
Jul 23 2017 02:45

function update(id, prop, value) {
if(value!=' '&&prop!='tracks')
collectionCopy[id][prop]=value;
else if(value!=' '&&prop=="tracks")
collectionCopy[id][prop].push(value);
else if(value==" ")
delete collectionCopy[id][prop];

return collection;
}

I can't get this over ):

kumquatfelafel
@kumquatfelafel
Jul 23 2017 02:45

@weifo For future: if you write it in a code block, it makes it easier to read and allows you to do some formatting. To write it in a code block...
for one line :point_right: ` code goes here `
or for multiple lines :point_down:
```js
code goes here (important: on a separate line)
```

Also, note that``` ≠ '''
You can also edit your post if you make a mistake by clicking the … that appears when you hover your mouse over your comment

Just going to format this so a little easier to read.
function update(id, prop, value) {
 if(value!=' '&&prop!='tracks')
   collectionCopy[id][prop]=value;
 else if(value!=' '&&prop=="tracks")
   collectionCopy[id][prop].push(value);
 else if(value==" ") 
   delete collectionCopy[id][prop];

 return collection;
}
kumquatfelafel
@kumquatfelafel
Jul 23 2017 02:52
@weifo what's up with all this else if (value == ' ')?
Joseph
@revisualize
Jul 23 2017 02:52
@weifo You shouldn't be working with collectionCopy but with the collection directly.
@weifo If you change collectionCopy and return collection you're not actually modifying what you're returning.
weifo
@weifo
Jul 23 2017 02:53
ok ,I' ll try,thanks a lot
Joseph
@revisualize
Jul 23 2017 02:54
@weifo You're missing some code too.
@weifo If there is no tracks array you need to create it before you can .push to it.
kumquatfelafel
@kumquatfelafel
Jul 23 2017 02:55
:point_up:
Also, " " != ""
@weifo in case point I was getting at before too obscure. :p
weifo
@weifo
Jul 23 2017 02:57
in this case---update(2548, "artist", "") , It failed,why?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 02:58
@weifo " " is not an empty string. It is a string with a space in it. Throughout your code (assuming you posted accurately), you are checking for a string with a space, which is incorrect.
That isn't necessarily the only reason it fails, but it is one of them.
kumquatfelafel
@kumquatfelafel
Jul 23 2017 03:06
I would make all the corrections @revisualize pointed out as well if you haven't yet.
Adam Faraj
@adamfaraj
Jul 23 2017 03:23
my random quote machine doesn't work anymore?
it was working a month ago
i get this error message in the console
Bashir Harrell
@bookofbash
Jul 23 2017 03:23
What message?
Adam Faraj
@adamfaraj
Jul 23 2017 03:23
GET https://www.quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_=1500780187326 net::ERR_INSECURE_RESPONSE jquery.min.js:4
korzo
@korzo
Jul 23 2017 03:31
@adamfaraj Change url to http
Adam Faraj
@adamfaraj
Jul 23 2017 03:32
@korzo which URL?
$(".quote__container--generator-btn").on("click", function() {
  $.ajax({
    method: "GET",
    url:
      "https://www.quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1",
    cache: false,
    dataType: "json"
korzo
@korzo
Jul 23 2017 03:33
@adamfaraj http://www.quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1
Adam Faraj
@adamfaraj
Jul 23 2017 03:34
@korzo XMLHttpRequest cannot load http://www.quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_=1500780850246. The 'Access-Control-Allow-Origin' header has a value 'http://null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.
korzo
@korzo
Jul 23 2017 03:37
@adamfaraj Strange. It works for me, when I change it in your code
Adam Faraj
@adamfaraj
Jul 23 2017 03:39
changing it in my main.js file?
korzo
@korzo
Jul 23 2017 03:39
@adamfaraj Yes, line 26
Adam Faraj
@adamfaraj
Jul 23 2017 03:45
@korzo had to empty my cache and hard reload
but it works now
thanks!
@korzo thank you
CamperBot
@camperbot
Jul 23 2017 03:46
adamfaraj sends brownie points to @korzo :sparkles: :thumbsup: :sparkles:
:cookie: 291 | @korzo |http://www.freecodecamp.com/korzo
Moisés Man
@moigithub
Jul 23 2017 03:48

@shivamg11000 try (<li key={todo.id}>{JSON.stringify(todo)}</li>)

its telling u,, it dont like to render objects

ncaught Error: Objects are not valid as a React child

korzo
@korzo
Jul 23 2017 03:53
@adamfaraj Glad to help
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:05

need help trying to figure this out just a hint no answers pls // Setup
function phoneticLookup(val) {
var result = "";

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

// Change this value to test
phoneticLookup("charlie");

i know i have to change the val but that was just a test statement
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:06
@Astewart400 You don't have to change val.
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:07
the lookup.alpha part
it should be lookup.something
right
Jianhao Tan
@jaanhio
Jul 23 2017 05:08
anyone familar with marked markdown parser?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:08

so... lookup.something is only fine if you want to search for the key "something" in lookup.

You want to search for... whatever val is.

just a reminder: val is assigned the value passed in when you call the function.
phoneticLookup("charlie"); // val will be "charlie"
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:13
ok let me try something
thanks
ok sooo lol why doest lookup.val work lol i got it to run with lookup[val]
@kumquatfelafel and thanks for the help
CamperBot
@camperbot
Jul 23 2017 05:15
astewart400 sends brownie points to @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:cookie: 525 | @kumquatfelafel |http://www.freecodecamp.com/kumquatfelafel
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:16

say I do something like this...

var blah = "alpha";
result = lookup.blah;

This will actually not look for "alpha". This will search for "blah".

We need to let it know that we are searching for the value assigned to the variable blah. In order to do this, we need to use bracket notation, like so.

lookup[blah]

@Astewart400
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:18
ok makes sense
im just happy i had the right idea in aa way just needed to fix where i was lookingup
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:19
basically, lookup.blah is equivalent to lookup["blah"], whereas lookup[blah] is equivalent to lookup["alpha"] (assuming blah is assigned alpha)
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:21
ooooooooooooooooo @kumquatfelafel thanks for the breakdown man!!!!
CamperBot
@camperbot
Jul 23 2017 05:21
astewart400 sends brownie points to @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:warning: astewart400 already gave kumquatfelafel points
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:22
@Astewart400 another instance you have to use the bracket notation is when the object key starts with a number. I'm pretty sure... but don't quote me on that. I'm drunk. :p ;)
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:22
lol
take one for me
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:23
"alpha": "Adams"
key :point_up: value :point_up:
(forget if lessons mention that, but good to know)
silver537
@silver537
Jul 23 2017 05:25
lessons do mention that.
just gotta read more carefully
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:25
they do
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:29
@silver537 's not really a matter of reading carefully. 's a matter of opening a new window/tab, navigating the map, trying to find the specific lesson where they first mention the concept, and seeing if it's there. I'm far too lazy to do that. I'd much rather launch into a long-winded and exhaustive explanation detailing why I daren't waste the effort. :p
;)
@Astewart400 though yeah, if key name starts with number, you can't use dot notation, you have to use bracket.
silver537
@silver537
Jul 23 2017 05:34
or you can just send a link to MDN :D
Stanley Su
@stanley-su
Jul 23 2017 05:34
@kumquatfelafel why does it work like that?
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:34
lol
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:35
@silver537 But that's like... three letters! :o
I'm not going to type three letters
silver537
@silver537
Jul 23 2017 05:35
but you rather typpe more?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:36

You have blown my mind.

....

Touché.

:p
@stanley-su Sorry, why does what work like what? :laughing:
silver537
@silver537
Jul 23 2017 05:36
lol
Stanley Su
@stanley-su
Jul 23 2017 05:39
@kumquatfelafel Why doesn’t dot notation work with numbers?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:45
not entirely sure if there's a more poignant reason than an adopted convention. It may also be something like "the moment we see a token that starts with a number, let's treat it like a number" What I can say is that with variable names, you can't start with a number.
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:47

is there a better way to get the result here or no
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}

else{
return 'Not Found';
}

return "Change Me!";
}

// Test your code by modifying these values
checkObj("gift");

kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:51

@Astewart400 well... you could technically just say something like...

function checkObj(checkProp) {
  return myObj[checkProp] ? myObj[checkProp] : "Not Found";
}

That being said, those unfamiliar with coercion and the conditional operator would have no idea what this means. :p

王想想
@wxxxxxxxx
Jul 23 2017 05:53

for(var i=0;i<arr.length;i++){
for(var j=0;j<arr[i].length; j++){
product *= arr[i] [j];

}

}

do not understand
Who can explain the next
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:55
@kumquatfelafel lmao tf
let me see if codecamp will take that
kumquatfelafel
@kumquatfelafel
Jul 23 2017 05:57

@Astewart400 as a matter of fact, you could also do...

return myObj[checkProp] || "Not Found";

Though the reverse : return "Not Found" || myObj[checkProp]; is not acceptable.

Stanley Su
@stanley-su
Jul 23 2017 05:57
@wxxxxxxxx Could you repaste the code, it’s confusing
@wxxxxxxxx But from what I can see, it’s just getting the product of all the numbers in a 2d array
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 05:58
it makes sense but i need to know how
王想想
@wxxxxxxxx
Jul 23 2017 05:58

function multiplyAll(arr) {
var product = 1;

for(var i=0;i<arr.length;i++){
for(var j=0;j<arr[i].length; j++){
product *= arr[i] [j];
}
}

return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

I do not understand the two-dimensional array
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 06:00
@kumquatfelafel im bout to check out w3schoo;s and mdn for the how
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:00
@Astewart400 in both the shorter examples I provided, there are a couple things going on that you're probably not entirely familiar with at this point.
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 06:01
not familiar but i understand what its doing
its a shorthand version of an if statement like
if the expression doesnt pass true it will do the other part of the code
ie
Stanley Su
@stanley-su
Jul 23 2017 06:02
@wxxxxxxxx it’s an array of arrays
so in multiplyAll([[1,2],[3,4],[5,6,7]]):
[[1,2],[3,4],[5,6,7]] is the arr
[1, 2] is arr[0]
[3, 4] is arr[1]
[5, 6, 7] is arr[2]
the first for loop in the function loops through arr
the inner for loop loops through arr[0], arr[1], and arr[2]
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 06:03
return basketball[best_game_ever] || "kill yourself'';
王想想
@wxxxxxxxx
Jul 23 2017 06:03
arr[i] Is all?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:10

i : :arrow_right:
j : :arrow_down:

arr[0][0] arr[1][0] arr[2][0] ... arr[arr.length - 1][0]
arr[0][1] arr[1][1] arr[2][1] ... arr[arr.length - 1][1]
...
arr[0][arr[0].length - 1] arr[1][arr[1].length - 1] arr[2][arr[2].length - 1] ... arr[arr.length - 1][arr[arr.length - 1].length - 1]

where arr[i][j]
Brian
@BrianCodes33
Jul 23 2017 06:13
tripleTrouble("this","test","lock"), "ttlheoiscstk”)
function tripleTrouble(one, two, three){
  var args = [...arguments];
  var result;
  for (var i = 0; i < args.length; i++) {
  }
  return result;
}
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:13
@wxxxxxxxx for loop provides a convenient way to go through the elements of arrays/subarrays by incrementing the index in each iteration.
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:19
@BrianCodes33 I'm not entirely sure what you're trying to do? The idea is to repeatedly take the first "remaining letter" of each of the following words and add it to result? Is there any more to the problem?
Brian
@BrianCodes33
Jul 23 2017 06:20
no that is it
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:20
Do you have to handle scenario where one word is much longer than the others, e.g., in a certain way? @BrianCodes33
Brian
@BrianCodes33
Jul 23 2017 06:21

Create a function that will return a string that combines all of the letters of the three inputed strings in groups. Taking the first letter of all of the inputs and grouping them next to each other. Do this for every letter, see example below!

Ex) Input: "aa", "bb" , "cc" => Output: "abcabc"

a7n007
@a7n007
Jul 23 2017 06:21
what is problem with my above code
Johnny
@JohnnyBizzel
Jul 23 2017 06:22
@BrianCodes33 Another loop over the chars of each string then inside the main loop.
a7n007
@a7n007
Jul 23 2017 06:22
it says potential harm of infinite loop
Pjdaze
@Pjdaze
Jul 23 2017 06:22
hello coders queation.. when a constructor function is being created and lets say we put 4 parameters in it .. is that always just like a starting point which i should assume its going to grow later on??
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:23

@a7n007 this is really difficult to read. Could you put your code into a code block?

```js
code goes inside here
```

Brian
@BrianCodes33
Jul 23 2017 06:24
@JohnnyBizzel ?
a7n007
@a7n007
Jul 23 2017 06:24
sure
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:24
@a7n007 you can edit your post by clicking the ... that appears in top right when you hover your mouse over comment
Pjdaze
@Pjdaze
Jul 23 2017 06:24
because lets say i got an object i want to create and lets say i know its going to have 20 keys.. i know it would look weird to have 20 arguments inside the parenthesis . is it smart to not have any parameters at all ??
Krishna Biradar
@kr5hn4
Jul 23 2017 06:30
@Pjdaze you make your function to take an object as an argument, and put all the arguments as key, value pairs inside that object
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:30

@Pjdaze Technically, no function needs specified parameters because you can get things from arguments, but that doesn't mean it's a good idea to have no parameters, especially when you're supposed to be "passing 20 arguments".

it would look weird to have 20 arguments

You could also combine some of these parameters into an object itself and pass the object instead.

For example, say I have parameters title, firstName, lastName and suffix. Instead of passing these separately, I could create a "name" object and pass that.... and have only one parameter: name

Krishna Biradar
@kr5hn4
Jul 23 2017 06:32
just like he said^
a7n007
@a7n007
Jul 23 2017 06:35

function convertToRoman(num) {
  var str=[];
  var mainstr=[];
    var i,ch1,ch2,ch3,x;
  while(num!=0)
    {
      ch1=num%10;
      str.push(ch1);
      num-=ch1;
      num/=10;
    }
  index=str.length;
  // 
    for(i=str.length-1;i>=0;i--)
      {
index--;
           // PLACE VALUE OF VAR
if(index===0)          // ones
{
ch1="I";
ch2="V";
ch3="X";
}
else if(index===1)  // tens
{
ch1="X";
ch2="L";
ch3="C";
}
else if(index==2) // hundreds
{
ch1="C";
ch2="D";
ch3="M";
}
else if(index==3) // thousands
{
ch1="M";
ch2="y";
ch3="z";
}
        else
          {
ch1="I";
ch2="V";
ch3="X";

          }
        x=str[i];
//x IS THE NUMBER AT THAT PLACE VALUE
 if(x>=1&&x<4)                        //the main converter
  {
    for(i=0;i<x;i++)
      mainstr=mainstr+ch1;   //ch1='I'
  }
  else if(i==4)
    mainstr=mainstr+ch1+ch2; ///ch2='V'
else if(i>4&&i<9)
{
mainstr=mainstr+ch2;
for(i=5;i<x;i++)
mainstr=mainstr+ch1;
}
else if(i==9)
mainstr=mainstr+ch1+ch2;
else
mainstr=mainstr;

}//for loop ends 
return mainstr;
}
convertToRoman(36);
says potential harm for an infinite loop
but i dont understand why
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:40
using the same variable (where value is shared) in a bunch of nested for loops is pretty much a recipe for disaster.
kumquatfelafel
@kumquatfelafel
Jul 23 2017 06:52
@a7n007 consider the following scenario.
var i = 10;
var j = 10;

for (i = "Hello".length - 1; i >= 0; i--) {
   console.log(i); //what is the value of i here?
   for (i = 0; i < j; i++);
   console.log(i); //what is the value of i here?
is this safe? Why or why not? @a7n007
Ronald T. Casili
@nvlled
Jul 23 2017 07:06
@a7n007 consider doing while (num > 0) instead, because of imprecisions involved in floating point computations, so you may not get a 0 exactly, but something like 0.000000000001
kumquatfelafel
@kumquatfelafel
Jul 23 2017 07:15
@nvlled While there is validity to this concern, you don't exactly want it to run through loop again when num == 0.0000000001 either. It's simpler to have something like ....
num = Math.floor(num/10); as opposed to num /= 10;
kumquatfelafel
@kumquatfelafel
Jul 23 2017 07:29
@BrianCodes33 sorry, got distracted. still need help/still around?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 07:36
@a7n007 other issues include: returning an array when you're supposed to be returning a string, trying to add characters to an array using the + operator, etc.
Huỳnh Trần Khanh
@khanh2003
Jul 23 2017 07:53
How do I find mouse position relative to <canvas> element?
Huỳnh Trần Khanh
@khanh2003
Jul 23 2017 08:20
@heroiczero thanks for the link.
const mousePosition = (element, event) => {
    const rect = element.getBoundingClientRect();
    const normal = {
        x: (event.clientX - rect.left) / rect.width * element.width,
        y: (event.clientY - rect.top) / rect.height * element.height
    };
    const webgl = {
        x: (normal.x / rect.width) * 2 - 1,
        y: - (normal.y / rect.height) * 2 + 1
    }
    return {normal, webgl};
}
CamperBot
@camperbot
Jul 23 2017 08:20
khanh2003 sends brownie points to @heroiczero :sparkles: :thumbsup: :sparkles:
:star2: 1519 | @heroiczero |http://www.freecodecamp.com/heroiczero
Sachin
@sachinmc
Jul 23 2017 09:34

hi folks, noob question,

> var stuff = new Array(10)
undefined
> stuff["a"] = 1;
1
> stuff
[ , , , , , , , , , , a: 1 ]
>

as you can see, there is an key-value pair style entry. I didn't know this was possible (used obj literals), what do you call this ..?

Stanley Su
@stanley-su
Jul 23 2017 09:34
arrays are actually objects
Sachin
@sachinmc
Jul 23 2017 09:35
@stanley-su my query is on the syntax and its use :)
Stanley Su
@stanley-su
Jul 23 2017 09:36
I don’t think there’s any use to doing that with an array
Sachin
@sachinmc
Jul 23 2017 09:36
hmm
@stanley-su I can use the array in this form to store key value pairs
Joseph
@revisualize
Jul 23 2017 09:36
@sachinmc As @stanley-su state. Arrays are actually objects.
Alexander Køpke
@alexanderkopke
Jul 23 2017 09:38
you can also add properties to functions but I haven't seen anyone do that yet in production but one could. @sachinmc
Sachin
@sachinmc
Jul 23 2017 09:38
@revisualize @stanley-su could you explain a bit more in this context please. I understand array is an object, and I am creating one with a constructor here, but not sure how that is relevant to the question around the key value pair use in []
@alexanderkopke ok
Joseph
@revisualize
Jul 23 2017 09:41
@sachinmc
var stuff = new Array(3).fill(2);
stuff["a"] = 1;
console.log(stuff);
for (var i in stuff) {
   console.log(stuff[i]);
}
console.log(stuff["a"]);
console.log(Object.keys(stuff));
stuff.push("b");
console.log(stuff);
console.log(Object.keys(stuff));
@sachinmc Basically, you should NOT be using a key value pair in an array.
alpox
@alpox
Jul 23 2017 09:50
@sachinmc If you want a real key/value store use Map
Sachin
@sachinmc
Jul 23 2017 09:51

@revisualize

stuff["a"] = 1
.
.
> Object.keys(stuff)
[ '0', '1', '2', '3', 'a' ]
>

when queried for the keys, it's returned a instead of the index position in that array. So the array object can accept a property a with a value 1, where the key is a and value is 1 ..

alpox
@alpox
Jul 23 2017 09:51
@sachinmc Yes because an array is also an object. BUT the array is not made for this kind of use at all
Sachin
@sachinmc
Jul 23 2017 09:52
@alpox ok cool
Joseph
@revisualize
Jul 23 2017 09:52
@sachinmc Again, you should NOT be using arrays as key value pairs.
That is NOT what they are for.
alpox
@alpox
Jul 23 2017 09:53
@sachinmc So when you as example use methods of an array like .map it will just ignore every special key/value pair. Also, adding a key/value pair like this does NOT increase the array .length
Sachin
@sachinmc
Jul 23 2017 09:56
@alpox interesting, I just tried that. how can we explain the array length not increasing when adding values like so: stuff["a"] = 1
alpox
@alpox
Jul 23 2017 09:57
@sachinmc Because an array consists of only number indices. Adding a string index just adds itself to the array object but doesn't count as an array item at all
Its the same as you would do:
function foo() {
}

foo["a"] = 1;
Joseph
@revisualize
Jul 23 2017 09:58
@sachinmc Do Object's have .lengths?
alpox
@alpox
Jul 23 2017 09:58
Because foo is also an object (A function is always also an object) you can do this. But its not meant to be done @sachinmc
silver537
@silver537
Jul 23 2017 09:58
yes
alpox
@alpox
Jul 23 2017 09:58
@silver537 no
silver537
@silver537
Jul 23 2017 10:00
right
Joseph
@revisualize
Jul 23 2017 10:00
image.png
silver537
@silver537
Jul 23 2017 10:00
my bad
Joseph
@revisualize
Jul 23 2017 10:01
Anyhow, this conversation is frustrating me. So, I'm out.
Sachin
@sachinmc
Jul 23 2017 10:01
@alpox ah makes sense, thanks ;)
CamperBot
@camperbot
Jul 23 2017 10:02
sachinmc sends brownie points to @alpox :sparkles: :thumbsup: :sparkles:
:star2: 1274 | @alpox |http://www.freecodecamp.com/alpox
Sachin
@sachinmc
Jul 23 2017 10:03
@alpox can you point me towards where I can read more on this ..
the question was on, use of functions and objects, I can google that
Joseph
@revisualize
Jul 23 2017 10:03
There isn't anywhere!
alpox
@alpox
Jul 23 2017 10:03
:point_up:
Stanley Su
@stanley-su
Jul 23 2017 10:03
You’re not going to need it.
Joseph
@revisualize
Jul 23 2017 10:03
You're breaking stuff and you want explanations about why it's broken.
Don't break stuff.
It's like buying a vase ... breaking it and not understanding why it won't hold water.
Functions are Objects.
Arrays are Object
silver537
@silver537
Jul 23 2017 10:05
lol
short tempered as always
Joseph
@revisualize
Jul 23 2017 10:05
in JavaScript almost everything is an Object.
Huỳnh Trần Khanh
@khanh2003
Jul 23 2017 10:05
@revisualize except primitive types
Joseph
@revisualize
Jul 23 2017 10:05
Hence. almost.
But, yeah.
Marco Galizzi
@Tezenn
Jul 23 2017 10:06
hi guys i have a problem in an array of arrays i want to push another array if is not already there ut this doesn't work, why???
var x = [1,1];
var z = [[2,3],[4,5],[1,1]];
if (z.indexOf(x) == -1){
  z.push(x);
}
console.log(z);
//it still push the x array
Sachin
@sachinmc
Jul 23 2017 10:07
@Tezenn that returns 2
Stanley Su
@stanley-su
Jul 23 2017 10:07
@Tezenn Probably better if you compare it manually
Sachin
@sachinmc
Jul 23 2017 10:07
@Tezenn you probably meant !=-1 ?
alpox
@alpox
Jul 23 2017 10:07
@Tezenn indexOf compares with === and that comparison compares the INSTANCE of an array - so [1,1] === [1,1] is false
kumquatfelafel
@kumquatfelafel
Jul 23 2017 10:08
@Astewart400 hey, so what I mentioned earlier, looking back slightly more sober was not quite correct. Or rather, was wrong for what you were trying to do. But I'm kinda too lazy/tired to explain why at the moment. Just... behaves similarly, but take it with a grain of salt for the time being.
Sachin
@sachinmc
Jul 23 2017 10:08
@Tezenn yea ignore me :D
alpox
@alpox
Jul 23 2017 10:08
@Tezenn it would work if you had:
var x = [1,1];
var z = [[2,3],[4,5],x];
Because then it would see that those arrays are the same instance
kumquatfelafel
@kumquatfelafel
Jul 23 2017 10:11
@Tezenn when you compare arrays with === it looks at to see if they're referencing the same location in memory (i.e. pointing to the exact same object).
Marco Galizzi
@Tezenn
Jul 23 2017 10:11
ok alright so how can i say don't push x to z if in z there is already a [1,1]?
alpox
@alpox
Jul 23 2017 10:11
@Tezenn You have to loop over the inner array to see if it contains all the same items as the other array
Marco Galizzi
@Tezenn
Jul 23 2017 10:12
ok thanks guys all this stuff is already inside another if :D @alpox @kumquatfelafel
CamperBot
@camperbot
Jul 23 2017 10:12
tezenn sends brownie points to @alpox and @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:cookie: 526 | @kumquatfelafel |http://www.freecodecamp.com/kumquatfelafel
:star2: 1275 | @alpox |http://www.freecodecamp.com/alpox
kumquatfelafel
@kumquatfelafel
Jul 23 2017 10:12
"poiny_up:
alpox
@alpox
Jul 23 2017 10:13
!z.some(arr => arr.every((num, i) => num === x[i])) would be my input but you may better go to find your own solution :-)
kumquatfelafel
@kumquatfelafel
Jul 23 2017 10:13
ehat @camperbot alpox said.
ugh. My mind is functioning but i chant' type :laughing:
alpox
@alpox
Jul 23 2017 10:14
@kumquatfelafel hangover is stuck in the fingers? :D
Marco Galizzi
@Tezenn
Jul 23 2017 10:15
i don't know yet the new way of writing js => etc
Marco Galizzi
@Tezenn
Jul 23 2017 10:16
@sachinmc thanks i'll check it out!
CamperBot
@camperbot
Jul 23 2017 10:16
tezenn sends brownie points to @sachinmc :sparkles: :thumbsup: :sparkles:
:cookie: 260 | @sachinmc |http://www.freecodecamp.com/sachinmc
Sachin
@sachinmc
Jul 23 2017 10:18
@Tezenn :)
kumquatfelafel
@kumquatfelafel
Jul 23 2017 10:18
@alpox haha. more or less.
...
There was more i was going to say, but I don't feel like typing it
alpox
@alpox
Jul 23 2017 10:23
@Tezenn There is no need to learn it in early stage. If you don't know it yet, better skip it and do it your usual way which you know
@kumquatfelafel :D give your fingers some rest then :D
kumquatfelafel
@kumquatfelafel
Jul 23 2017 10:31
arrow is bit like, if you want to think of it from a rather low-level standpoint, an excuse not to write the word "function" and... in case of one-liners, "return"
@alpox Never!!! :o
...
:zzz:
alpox
@alpox
Jul 23 2017 10:32
@kumquatfelafel So... arrow functions are good for your fingers - less typing :D
Sulaiman
@suli-g
Jul 23 2017 11:00

Finished my URL shortener this morning (6am) all-nighter:

here are the links:
the deployed version
github repo

Stephen James
@sjames1958gm
Jul 23 2017 11:30
@suli-g :+1:
Kshitijaa Jaglan
@KshitijaaJaglan
Jul 23 2017 12:15
hello people.
Any idea why this code is returning
```
 [27,0,39,1001]
instead of
[27,5,39,1001]
function largestOfFour(arr) {
   var numberaccessed=0;
  var bigarray=[];
  var numberaccessedarr;
  for(i=0;i<arr.length;i++){ 
    numberaccessedarr=0;
    for(j=0;j<arr[i].length;j++){

      if(numberaccessed<arr[i][j]){
        numberaccessed=arr[i][j];
        numberaccessedarr=numberaccessed;
      }

    }

    bigarray=bigarray.concat(numberaccessedarr);
  }
  return bigarray;
}
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
for the algorithm challenge return largest number in array
Sweet Coding :)
@SweetCodingInc
Jul 23 2017 12:20
@KshitijaaJaglan You also need to re initialize numberaccessed inside first for loop
Stephen James
@sjames1958gm
Jul 23 2017 12:20
@KshitijaaJaglan You are resetting the largest value inside the the loop
Sweet Coding :)
@SweetCodingInc
Jul 23 2017 12:21

So

for(i=0;i<arr.length;i++){ 
    numberaccessedarr=0;

should be

for(i=0;i<arr.length;i++){
    numberaccessed  = 0; 
    numberaccessedarr=0;
Stephen James
@sjames1958gm
Jul 23 2017 12:21
@KshitijaaJaglan Not sure why you have two variables both numberaccessed and numberaccessedarr
Sweet Coding :)
@SweetCodingInc
Jul 23 2017 12:21
Yeah.. You don't really need that numberaccessedarr
you can just do bigarray.push(numberaccessed ) instead of bigarray=bigarray.concat(numberaccessedarr);
Kshitijaa Jaglan
@KshitijaaJaglan
Jul 23 2017 12:23
Thanks a lot @SweetCodingInc @sjames1958gm it worked!!!
CamperBot
@camperbot
Jul 23 2017 12:23
kshitijaajaglan sends brownie points to @sweetcodinginc and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:cookie: 149 | @sweetcodinginc |http://www.freecodecamp.com/sweetcodinginc
:star2: 8138 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Kshitijaa Jaglan
@KshitijaaJaglan
Jul 23 2017 12:24
@SweetCodingInc but it says that bigarray.push is not a function
Sweet Coding :)
@SweetCodingInc
Jul 23 2017 12:25
@KshitijaaJaglan Let me guess, you're doing bigarray = bigarray.push(numberaccessed )?
if so, don't do it. Replace that entire line with just bigarray.push(numberaccessed )
because .push returns length of bigarray after you push an element to it
so you change type of bigarray from Array to number
Kshitijaa Jaglan
@KshitijaaJaglan
Jul 23 2017 12:28
Ah... so that was it
Michiel
@MichielHuijse
Jul 23 2017 12:53
Is the way how I invoke the filterFunction correct? I am currently getting an empty array so I am not really sure.
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
    var sourceKeys = Object.keys(source);
  arr = collection.filter(filterFunction());

    function filterFunction() {
        for (var i = 0; i < sourceKeys.length; i++) {
            if (collection.hasOwnProperty(sourceKeys[i])) {
                return true;
            } else {
                return false;
            }
        }
}

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

whatIsInAName([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 });
cowCrazy
@cowCrazy
Jul 23 2017 13:04
@MichielHuijse look at ths example on mdn, you should pass parameters and arguments to the filter function.
Michiel
@MichielHuijse
Jul 23 2017 13:07
Hi @cowCrazy thanks, I already changed it to something new.
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
    var sourceKeys = Object.keys(source);


        for (var i = 0; i < sourceKeys.length; i++) {
            if (collection.hasOwnProperty(sourceKeys[i])) {
                 arr = collection.filter(sourceKeys[i]);
            } else {
                return false;
            }
        }


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

whatIsInAName([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 });
CamperBot
@camperbot
Jul 23 2017 13:07
michielhuijse sends brownie points to @cowcrazy :sparkles: :thumbsup: :sparkles:
:cookie: 302 | @cowcrazy |http://www.freecodecamp.com/cowcrazy
cowCrazy
@cowCrazy
Jul 23 2017 13:18
@MichielHuijse you still need help or you wanna try on your own first?
Stephen James
@sjames1958gm
Jul 23 2017 13:18
@MichielHuijse filter requires a function you are passing it a value
@MichielHuijse Also, collection an array - so hasOwnProperty on an array here, doesn't make much sense
Michiel
@MichielHuijse
Jul 23 2017 13:22
Thanks @sjames1958gm thanks, I will take some time to figure it out myself. this will help me on my way
CamperBot
@camperbot
Jul 23 2017 13:22
michielhuijse sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8140 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Stephen James
@sjames1958gm
Jul 23 2017 13:22
@MichielHuijse :+1:
Manish Chandra
@chandrajob365
Jul 23 2017 13:45
var chkFunc = function () {
  let name = 'Manish'
return {
  getName: function () {
    console.log('Name is Manish')
    testName('Rahul')
  },
  testName: function (name) {
    console.log('Now name is : ', name)
  }
}
}

chkFunc().getName()
Above giving error as testName not defined
Joel Y.
@zapcannon99
Jul 23 2017 13:47
I've never seen function defined like that O__O
functions*
Stephen James
@sjames1958gm
Jul 23 2017 13:50
@chandrajob365 testName doesn't exist as a unqualified name.
You could use this.testName
Joel Y.
@zapcannon99
Jul 23 2017 13:51
I'm a noob. this would then be referring to the object getting returned right?
Ronique Ricketts
@RoniqueRicketts
Jul 23 2017 13:52
@zapcannon99 yes
Siphils
@Siphils
Jul 23 2017 13:52

DNA Pairing

There is my method.

function pair(str) {
    var arr = str.split("");
    var pairs = {
        "A": "T",
        "T": "A",
        "G": "C",
        "C": "G"
    };
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
        newArr.push([arr[i], pairs[arr[i]]]);
    }
    return newArr;
}

Is there a simpler way?

Ronique Ricketts
@RoniqueRicketts
Jul 23 2017 13:52
Joel Y.
@zapcannon99
Jul 23 2017 13:53
@sjames1958gm @RoniqueRicketts Thanks. That was enlightening. @chandrajob365 Thanks for reminding me how to pass functions into objects (or whatever the term was)
CamperBot
@camperbot
Jul 23 2017 13:53
zapcannon99 sends brownie points to @sjames1958gm and @roniquericketts and @chandrajob365 :sparkles: :thumbsup: :sparkles:
:cookie: 301 | @roniquericketts |http://www.freecodecamp.com/roniquericketts
:cookie: 262 | @chandrajob365 |http://www.freecodecamp.com/chandrajob365
:star2: 8141 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Ghulam Shabir
@ghulamshabir
Jul 23 2017 13:54
@Siphils i used map and switch @Siphils
Manish Chandra
@chandrajob365
Jul 23 2017 13:55
@sjames1958gm @RoniqueRicketts Thank you . It worked . BUt this is very confusing . Not able to get hold of it :(
CamperBot
@camperbot
Jul 23 2017 13:55
chandrajob365 sends brownie points to @sjames1958gm and @roniquericketts :sparkles: :thumbsup: :sparkles:
:cookie: 302 | @roniquericketts |http://www.freecodecamp.com/roniquericketts
:star2: 8142 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Stephen James
@sjames1958gm
Jul 23 2017 13:55
@Siphils I also, used map and switch, but map and object would also work.
@chandrajob365 testName only exists inside of an object, so you have to reference the object to get to the function.
When you call myObj.getName() the this variable references myObj so inside of getName() this.testName refers to the correct function
Siphils
@Siphils
Jul 23 2017 13:58
@ghulamshabir @sjames1958gm I don't know how to use map. So I ussd object...
I will try that later.
Stephen James
@sjames1958gm
Jul 23 2017 13:58
@Siphils map would replace your for loop not your object.
Manish Chandra
@chandrajob365
Jul 23 2017 13:59
@sjames1958gm it seems silly , but this comes in picture when we create object using new?
Stephen James
@sjames1958gm
Jul 23 2017 13:59
@chandrajob365 Yes, but not only then. When you call a function "on" an object this also comes into play.
Manish Chandra
@chandrajob365
Jul 23 2017 15:30
@sjames1958gm Actually , that is the problem , which I am trying to solve . Working on creating a HTTP server using only net module of node. But as this is not right forum to ask , didn’t put it here
Michiel
@MichielHuijse
Jul 23 2017 16:09
How to compare the name of a property between two different array's?
alpox
@alpox
Jul 23 2017 16:14
@MichielHuijse arrays have no property names
except length and some functions :D
Michiel
@MichielHuijse
Jul 23 2017 16:15
you are right I meen objects. So it would be: How to compare the name of a property between two different objects?
alpox
@alpox
Jul 23 2017 16:16
@MichielHuijse you have an object of which you don't know the property names?
And do you want to test for a specific property name?
Or all of them
Michiel
@MichielHuijse
Jul 23 2017 16:17
Yes, I think I should save the property name in a var and then compare it with obj.hasOwnProperty(' ').
alpox
@alpox
Jul 23 2017 16:18
@MichielHuijse Hmm thats more a lookup if an object defines the given property name
not a comparison
Michiel
@MichielHuijse
Jul 23 2017 16:18
Yes it is.
*a look up.
alpox
@alpox
Jul 23 2017 16:22
@MichielHuijse Then you are right - to do it with hasOwnProperty
Michiel
@MichielHuijse
Jul 23 2017 16:23
@alpox thanks, will give it try :)
CamperBot
@camperbot
Jul 23 2017 16:23
michielhuijse sends brownie points to @alpox :sparkles: :thumbsup: :sparkles:
:star2: 1278 | @alpox |http://www.freecodecamp.com/alpox
makalohri
@makalohri
Jul 23 2017 16:41
@knrt10 thanks man
CamperBot
@camperbot
Jul 23 2017 16:41
makalohri sends brownie points to @knrt10 :sparkles: :thumbsup: :sparkles:
:cookie: 466 | @knrt10 |http://www.freecodecamp.com/knrt10
Gokula Krishna
@AKX-X-32
Jul 23 2017 17:39
Hey guys, completed my Pomodoro Timer please take a look and let me know your review. https://codepen.io/gkrishna/pen/JyPVpX?editors=1000
kumquatfelafel
@kumquatfelafel
Jul 23 2017 17:45
@AKX-X-32 currently, when your seconds is in single digits, it says e.g., 24 : 9.
Try to make it say 24 : 09 instead.
Gokula Krishna
@AKX-X-32
Jul 23 2017 17:46
@kumquatfelafel noted (y)
@kumquatfelafel done
kumquatfelafel
@kumquatfelafel
Jul 23 2017 17:52
okay... first transition to break appears to go properly
first transition to work go properly
works fine over multiple transitions.
Michiel
@MichielHuijse
Jul 23 2017 17:57
Hey hello, I am busy with where art thou. I solved 2 of the 4 challenges. However my comparison is not good enough. Except comparing values I should also check if the other property the source has exists. How do I go about it. Now an object with only one but not two of the same values is getting trough.
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
    var sourceKeys = Object.keys(source);
    var sourceValues = Object.values(source);
         for (var i = 0; i < sourceKeys.length; i++) {
            for (var j = 0; j < collection.length; j++) {
                 let arrVal = Object.values(collection[j]);
                for(var k = 0; k < arrVal.length; k++) {
                    if (arrVal[k] === sourceValues[i]) {
                 arr.push(collection[j]);
                }
        }
            }
        }

  // Only change code above this line
    console.log(arr);
    return arr;
}

whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });
Gokula Krishna
@AKX-X-32
Jul 23 2017 17:58
@kumquatfelafel thanks for the test
CamperBot
@camperbot
Jul 23 2017 17:58
akx-x-32 sends brownie points to @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:cookie: 527 | @kumquatfelafel |http://www.freecodecamp.com/kumquatfelafel
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:00
@MichielHuijse this seems like a lot of for loops.

@MichielHuijse say I have ...

{"a" : 1, "b" : 2 }
and
{"c" : 1, "d" : 2 }

Do these have the same values? How about the same keys?

Michiel
@MichielHuijse
Jul 23 2017 18:06
hmm okay, not the same keys indeed
Guderian Raborg
@hypercuber
Jul 23 2017 18:08

I did this function a while ago and it works:

function smallestMultipleOfOneTo(r) { //count r inside pList if prime?
  let arr = pList(r); //arr = 2, 3, 5, 7, 11, 13, 17, 19
  let exp = 1;
  for (let i = 0; i < arr.length; i++) {
    while (Math.pow(arr[i], exp) < r) {
      exp++;
    }
    exp -= 1;
    arr[i] = Math.pow(arr[i], exp);
    exp = 1;
  } //arr = 16, 9, 5, 7, 11, 13, 17, 19
  return arr.reduce((a,b) => a * b);
}

but then I tried to improve it and it doesnt work:

function smallestMultipleOfOneTo(r) {
  let arr = pArr(r); //arr = 2, 3, 5, 7, 11, 13, 17, 19
  let exp = 1;
  for (let i = 0; i < arr.length; i++) {
    while (arr[i] < r) { //include r?
      exp++; 
      arr[i] = Math.pow(arr[i], exp);
    }
    exp = 1;
  } //16, 9, 5, 7, 11, 13, 17, 19         <--- arr should be this ***
  return arr;
}
nvm it works now:
function smallestMultipleOfOneTo(r) {
  let arr = pArr(r); //arr = 2, 3, 5, 7, 11, 13, 17, 19
  let exp = 1;
  for (let i = 0; i < arr.length; i++) {
    while (Math.pow(arr[i], exp + 1) < r) { //include r?
      exp++;
    }
    arr[i] = Math.pow(arr[i], exp);
    exp = 1;
  } //16, 9, 5, 7, 11, 13, 17, 19         <--- arr should be this ***
  return arr;
}
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:16

@hypercuber you can have more than one initializer inside for loop. e.g.

for (let i = 0, exp = 1; i < arr.length; i++, exp = 1)

Pagnito
@Pagnito
Jul 23 2017 18:21
anyone good with svg animations?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:28

@hypercuber You could also potentially speed it up a bit if you do something like

while (Math.pow(arr[i], exp + 2) < r)
    exp += 2;
if (Math.pow(arr[i], exp + 1) < r)
    exp++;

As that will cut down on your Math.pow() calls and your increments to exp by about half.

I think this would speed it up (in most cases), anyway. Too lazy to actually test though. :p
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:37

^that being said, this will double your calls if exp should be 1 or 2. So no guarantee.

Basically, it's a matter of the size of r with relation to arr[i]

@Pagnito wish I could say yes, as it seems to be dead in here... but no
Adit
@adittyagi
Jul 23 2017 18:40
Anyone who could go through my code and identify the error ? The code is it not working for the other 3 arrays.
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:40
@adittyagi possibly. But won't know for sure until you post it. :p
Adit
@adittyagi
Jul 23 2017 18:41

Here you go!
function largestOfFour(arr)
{
// You can do this!
var max = [];
for(var i=0; i<arr.length; i++) { for(var j=0; j<arr[i].length; j++) { if(arr[i][j]>max)
{
max[i]=arr[i][j];
}
}

}
return [max[0],max[1],max[2],max[3]];
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);

kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:42
Why are there so many closing brackets???
Adit
@adittyagi
Jul 23 2017 18:42

Here you go!
function largestOfFour(arr)
{
// You can do this!
var max = [];
for(var i=0; i<arr.length; i++) { for(var j=0; j<arr[i].length; j++) { if(arr[i][j]>max)
{
max[i]=arr[i][j];
}
}

}
return [max[0],max[1],max[2],max[3]];
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);

kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:42
... oh

for(var i=0; i<arr.length; i++) { for(var j=0; j<arr[i].length; j++) { if(arr[i][j]>max)

For future, don't do this. Separate so that each is on it's own line.

Adit
@adittyagi
Jul 23 2017 18:43
The code got distorted when I pasted here
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:44
Just gonna reformat this a bit.
function largestOfFour(arr) 
{
  // You can do this!
  var max = [];
  for(var i=0; i<arr.length; i++)
  {
    for(var j=0; j<arr[i].length; j++) 
    {
      if(arr[i][j]>max) 
      {
        max[i]=arr[i][j];
      }
    }

  }
   return [max[0],max[1],max[2],max[3]];    
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
Adit
@adittyagi
Jul 23 2017 18:44
How to do this ?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:45
@adittyagi
```js
some code
```
becomes
some code
if(arr[i][j]>max)... what does this compare? @adittyagi
Adit
@adittyagi
Jul 23 2017 18:48
arr[i][j] are the values of sub-arrays I suppose which are compared to var of 0 value
the code only works for first sub array
the output is [9,13,null,null]
Moisés Man
@moigithub
Jul 23 2017 18:52
whats value have max ?
Adit
@adittyagi
Jul 23 2017 18:53
[9,13,null,null]
Moisés Man
@moigithub
Jul 23 2017 18:53
when ur code start
max is [ ]
Adit
@adittyagi
Jul 23 2017 18:53
0
Moisés Man
@moigithub
Jul 23 2017 18:54
no its not 0.. re-read ur code ...........
kumquatfelafel
@kumquatfelafel
Jul 23 2017 18:55
This link shows more or less what happens to your code as it progresses.
Adit
@adittyagi
Jul 23 2017 18:57
var max = 0;
when I set this there's an error 'Cannot create property '0' on number '0'
FlashHero
@FlashHero
Jul 23 2017 18:57
does the first parameter in the post method of express nodejs actually render the content in the file or is it just a name that has to match with html form action attribute
Adit
@adittyagi
Jul 23 2017 19:00
@kumquatfelafel still not getting it
Moisés Man
@moigithub
Jul 23 2017 19:03
divide n conquer thing...
first make a function to get the largest of a SINgle arrray : ie [1,2,3,4] should result 4
function max( arr ){
   var   result = 0;
    // fill the rest of code here
   return result;
}
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:05
@adittyagi you're trying to have the best of both worlds at the moment in your code, treating max as both a number and an array. That comparison you have at the moment works by converting array into a number. However, array can only be reliably changed to a number when it has a single numeric element or no elements at all. The second you put 13 into the next "slot", the array is converted to NaN during the comparison... which is more or less guaranteed to return false.
console.log(Number([])); // prints 0
console.log(Number([5])); // prints 5
console.log(Number([5, 7])) // prints NaN
Chris Juchtmans
@kjuchtmans
Jul 23 2017 19:17
evening coders! :wave:
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:17

@adittyagi We can also think of it this way.

Say we've gone through first iteration and have found our value for max[0]. Now, we're working on max[1].

When do we want to assign a new value to max[1]?

hey
Adit
@adittyagi
Jul 23 2017 19:18
ohhhhhhhhh okay
that means first I have to store the value of max[1] then the others
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:19
@adittyagi nope
Adit
@adittyagi
Jul 23 2017 19:19
multiple loops ?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:20
@adittyagi just focus on the question. When... under what conditions do we want to assign a new value to max[1]?
Adit
@adittyagi
Jul 23 2017 19:21
after comparing the values in 2nd sub array
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:21
comparing with what?
Adit
@adittyagi
Jul 23 2017 19:22
and assigning the max value to max[1]
comparing max with the numbers in 2nd sub array
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:23
@adittyagi Does max == max[1]?
Adit
@adittyagi
Jul 23 2017 19:23
no max was supposed to be 0 till now
there's a catch
baersc
@baersc
Jul 23 2017 19:26

Hey, guys I've got a function that's working, and solving the problem, but the linter is throwing a "Expected assignment or function call and instead saw an expression" error.
The line with the error is:

Array.isArray(arr[i]) ? flatten(arr[i]) : flat.push(arr[i]);

Should I change this code or ignore the error?

kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:27
never mind what max was supposed to be. Let's talk about what it is. max is an array. Is an array the same thing as a single value?
Adit
@adittyagi
Jul 23 2017 19:29
umm no
Amit Patel
@AmitP88
Jul 23 2017 19:30
hey guys, I'm currently studying the topic of functional (or modular) JS and I was wondering: Are higher-order functions and callbacks the same thing? In a sense that they both refer to functions that are used as arguments for other functions
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:30
how about max[1]? Is that a single value? @adittyagi
Chris Juchtmans
@kjuchtmans
Jul 23 2017 19:31

"Arguments Optional" lesson on closures & currying:
Question:
How can I access arguments (2) and (3), when the test reads addTogether(2)(3); ?
My below code returns a TypeError: 'addTogether( )( ) is not a function'

code:

function addTogether() {
  var a,b,
      result;

  a = arguments[0];
  b = arguments[1];
  //return typeof b;
  if (typeof a !== 'number'|| typeof b !== 'number'){
      result = undefined;
    } 

      else if(arguments.length === 2){
      result = a+b;
    } 

       else if (arguments.length === 1){
        var adderCurried = function(x){
          return function(y){
            return x+y;
            };
          };
        var addToTwo = adderCurried(2);
        result = addToTwo(3);
    } return result;
}  

addTogether(2)(3);

thanks!

Stuhl
@Stuhl
Jul 23 2017 19:31
@baersc Cannot give you a clear answer, but the error is pointing at the way u made the ternary. I think JSlints want you to assign the ternary to variable or something
baersc
@baersc
Jul 23 2017 19:32
@Stuhl Thanks, I'll read that
CamperBot
@camperbot
Jul 23 2017 19:32
baersc sends brownie points to @stuhl :sparkles: :thumbsup: :sparkles:
:cookie: 170 | @stuhl |http://www.freecodecamp.com/stuhl
Adit
@adittyagi
Jul 23 2017 19:32
@kumquatfelafel yes?
Joseph
@revisualize
Jul 23 2017 19:32
@kjuchtmans Currying
Chris Juchtmans
@kjuchtmans
Jul 23 2017 19:37
@revisualize thanks, I've been reading up on currying pretty much all PM, and I feel I grasp some of it now: my curried func adderCurried seems to return the correct resultm when isolated from the if ... else structure.
But when I try to implement it, the test line addTogether(2)(3) is not accepted.
Why is that?
CamperBot
@camperbot
Jul 23 2017 19:37
kjuchtmans sends brownie points to @revisualize :sparkles: :thumbsup: :sparkles:
:star2: 4373 | @revisualize |http://www.freecodecamp.com/revisualize
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:39
@adittyagi How about arr[1][1]? Is that a single value?
Joseph
@revisualize
Jul 23 2017 19:41
@kjuchtmans No idea.
@kjuchtmans Here's some testing I did.
Adit
@adittyagi
Jul 23 2017 19:41
yes that's a single value @kumquatfelafel
Joseph
@revisualize
Jul 23 2017 19:42

@kjuchtmans

function addTogether() {
    return 1;
}  
addTogether(2)(3);

Error.
However...

@kjuchtmans

function addTogether() {
    return function() { return 1; };
}  
addTogether(2)(3);

No error. and returns 1

Chris Juchtmans
@kjuchtmans
Jul 23 2017 19:43
@revisualize ok. I understand
Joseph
@revisualize
Jul 23 2017 19:43

@kjuchtmans

function addTogether() {
    return function() { return arguments[0]; };
}  

addTogether(2)(3);

returns 3

Chris Juchtmans
@kjuchtmans
Jul 23 2017 19:44
right. that indeed makes things clearer, cheers! will go and rework from here
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:44
@adittyagi just going to restart my computer, back in a sec
Joseph
@revisualize
Jul 23 2017 19:44

@kjuchtmans

function addTogether() {
    var a = arguments[0];
    return function() { 
                return a + arguments[0]; 
           };
}  

addTogether(2)(3);

5

Adit
@adittyagi
Jul 23 2017 19:44
okay btw thank you! @kumquatfelafel
CamperBot
@camperbot
Jul 23 2017 19:44
adittyagi sends brownie points to @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:cookie: 528 | @kumquatfelafel |http://www.freecodecamp.com/kumquatfelafel
Joel Y.
@zapcannon99
Jul 23 2017 19:45
Not that I said anything, but I learned something else again from you two. Thanks. @kjuchtmans @revisualize
CamperBot
@camperbot
Jul 23 2017 19:45
zapcannon99 sends brownie points to @kjuchtmans and @revisualize :sparkles: :thumbsup: :sparkles:
:cookie: 291 | @kjuchtmans |http://www.freecodecamp.com/kjuchtmans
:star2: 4374 | @revisualize |http://www.freecodecamp.com/revisualize
Tiago Correia
@tiagocorreiaalmeida
Jul 23 2017 19:45
hey
``

var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};

// print hello in the 3 different languages
for(var i =0; i<languages.length; i++){
if(typeof(languages[i]) === "string"){
console.log(languages[i]);
}

}

``
``
``
why the `` thing not working?
hum
Joseph
@revisualize
Jul 23 2017 19:46
@tiagocorreiaalmeida objects don't have lengths
Joel Y.
@zapcannon99
Jul 23 2017 19:46
it's ```
you need 3
@tiagocorreiaalmeida
Chris Juchtmans
@kjuchtmans
Jul 23 2017 19:46
so basically your accessing two different index [0]'s, belonging to two different functions, true?
Tiago Correia
@tiagocorreiaalmeida
Jul 23 2017 19:46
you are right
Joseph
@revisualize
Jul 23 2017 19:47

@tiagocorreiaalmeida

var myObj = {
     "a": "alpha",
     "b": "bravo"
}

What is the value of myObj.length;?

Tiago Correia
@tiagocorreiaalmeida
Jul 23 2017 19:47
I have to use like
for( var x in myobj)
to get the keys right?
Joseph
@revisualize
Jul 23 2017 19:47
You could.
Tiago Correia
@tiagocorreiaalmeida
Jul 23 2017 19:47
for(var values in languages){
like this
Joseph
@revisualize
Jul 23 2017 19:47
try it
Tiago Correia
@tiagocorreiaalmeida
Jul 23 2017 19:47
done thanks :)
kumquatfelafel
@kumquatfelafel
Jul 23 2017 19:52

@adittyagi okay. so we've established that arr[1][1] and max[1] are single values. max[1] is supposed to (at the end of iteration) contain the maximum value for that particular subarray.

Say we start with the assumption that max[1] is arr[1][0]... which in this (hypothetical) case happens to be 5.

If arr[1][1] is 3, do we want to change max[1]?
How about if arr[1][1] is 7? Do we change it then?

Stuhl
@Stuhl
Jul 23 2017 19:56
@revisualize Is there a reason why u hardcode arguments into the function instead of just making 2 parameters before hand ?
Adit
@adittyagi
Jul 23 2017 19:57
if arr[1][1] is 3 we will not change max[1] but if arr[1][1] is 7 we'll change max[1]
@kumquatfelafel
we need another variable to store max[0],max[1],max[2],max[3] values then which should be an array
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:01

if arr[1][1] is 3 we will not change max[1] but if arr[1][1] is 7 we'll change max[1]

@adittyagi So could we then generalize this a bit more in context of your actual code and say that if arr[1][j] is 3, we won't change max[1] (since max[1] is 5), and when arr[1][j] is 7, we'd change max[1] to 7?

Adit
@adittyagi
Jul 23 2017 20:02
yess
@kumquatfelafel
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:03
@adittyagi Don't worry about rest of max or anything for moment. How would you express what I just said above in an if statement?
Joseph
@revisualize
Jul 23 2017 20:05
@Stuhl What?
@Stuhl I didn't hard code arguments into the function.
@Stuhl The arguments object is built into every function.
Stuhl
@Stuhl
Jul 23 2017 20:07
@revisualize Yeah but its easier to specify the arguments in the function parameter, its easier that way and currying works fine with that too
Joseph
@revisualize
Jul 23 2017 20:07
@Stuhl I was just doing testing.
Adit
@adittyagi
Jul 23 2017 20:08
max[1] = arr[1][0];
if(arr[1][j]>max[1])
{
max[1]=arr[1][j];
  }  
Joseph
@revisualize
Jul 23 2017 20:08
@Stuhl Why am I being critiqued on some quick test code?
Stuhl
@Stuhl
Jul 23 2017 20:08
@revisualize Ah okay, didn't read the context from the complete beginning
Thought u were explaining currying
Joseph
@revisualize
Jul 23 2017 20:08
No.
Stuhl
@Stuhl
Jul 23 2017 20:09
Im not criticizing, it just looked confusing in the beginning
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:09
@adittyagi Did I say we were working off the assumption that max = arr[1][0]? I'm pretty sure I said we're assuming that max[1] = arr[1][0].
max != max[1]. Understanding this part is crucial to completing the task.
max is the entire array of values. max[1] is the second element of that array.
@adittyagi good!
Adit
@adittyagi
Jul 23 2017 20:12
oh sorry!
I hope it's better now
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:12
@adittyagi so this is... pretty close to what it looks like for max[1]. Now we just need to generalize it and take care of a few ... hiccups.

@adittyagi say we want to generalize this even more... make it work for any element of max. What could we do?

Note: if you were to copy paste this generalization and run your code, it still won't quite work in current state so don't be concerned

Michiel
@MichielHuijse
Jul 23 2017 20:15
What is obj here in this program? Is it an argument? And what is its initial value? I can't really understand it.
function whatIsInAName(collection, source) {
  // "What's in a name? that which we call a rose
  // By any other name would smell as sweet.”
  // -- by William Shakespeare, Romeo and Juliet
  var srcKeys = Object.keys(source);

  // filter the collection
  return collection.filter(function (obj) {
    for(var i = 0; i < srcKeys.length; i++) {
      if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
        return false;
      }
    }
    return true;
  });
}

// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:16
@MichielHuijse it's just an arbitrary variable name that holds current element being examined.
Joseph
@revisualize
Jul 23 2017 20:17
@MichielHuijse Object.keys()?
Adit
@adittyagi
Jul 23 2017 20:17
@kumquatfelafel max[i] ?
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:17
@adittyagi yup
Michiel
@MichielHuijse
Jul 23 2017 20:17
@kumquatfelafel aaah, ok, thanks,.
CamperBot
@camperbot
Jul 23 2017 20:17
:cookie: 529 | @kumquatfelafel |http://www.freecodecamp.com/kumquatfelafel
michielhuijse sends brownie points to @kumquatfelafel :sparkles: :thumbsup: :sparkles:
Joseph
@revisualize
Jul 23 2017 20:17
@MichielHuijse OH! obj that's the parameter you specified for your function.
@MichielHuijse the .filter() method passes each array element to the function as arguments[0]
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:19
@adittyagi So now we have...
max[i] = arr[i][0];
if(arr[i][j]>max[i])
{
   max[i]=arr[i][j];
 }
Adit
@adittyagi
Jul 23 2017 20:20
okay...
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:20
@adittyagi now... here's where the slightly awkward part comes in. We have to reincorporate this into our code.
Moisés Man
@moigithub
Jul 23 2017 20:21
@alenreggie what code u did ?
alenreggie
@alenreggie
Jul 23 2017 20:22
var quotient = 4.4 / 2.0; // Fix this line
Moisés Man
@moigithub
Jul 23 2017 20:22
it passed on my screen
Joel Y.
@zapcannon99
Jul 23 2017 20:23
@alenreggie Works for me too.
Adit
@adittyagi
Jul 23 2017 20:24
@kumquatfelafel
function largestOfFour(arr) 
{
  // You can do this!
  var result = [];

  for(var i=0; i<arr.length; i++)
  {
     var max = arr[i][0];
    for(var j=0; j<arr[i].length; j++) 
    {
      if(arr[i][j]>max) 
      {
        max = arr[i][j];

      }
    }
       result[i] = max;   
  }  

   return result;    

}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
this worked !! :smile:
alenreggie
@alenreggie
Jul 23 2017 20:25
Thanks to both of you.. I'll just skip it for now.. I've just read that this happens to some people..
Adit
@adittyagi
Jul 23 2017 20:25
max is now a single value
result is an array where new max will be stored
@kumquatfelafel thanks a lot ! :smile:
CamperBot
@camperbot
Jul 23 2017 20:26
adittyagi sends brownie points to @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:warning: adittyagi already gave kumquatfelafel points
Moisés Man
@moigithub
Jul 23 2017 20:26
@adittyagi u could initialize j to 1
for(var j=1; j<ar ....
..then u can figure out why :)
Adit
@adittyagi
Jul 23 2017 20:27
both are correct
why is it so ?
Moisés Man
@moigithub
Jul 23 2017 20:28
max already contains the first value of the subarray var max = arr[i][0];
no point on compare to itself...
starting j with 1 will make it compare to the next value
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:28

:point_up:
@adittyagi say i is 0 and j is 0.

we've just assumed that max = arr[0][0]. Now we're checking if arr[0][0] > max, aka arr[0][0]

Adit
@adittyagi
Jul 23 2017 20:30
@moigithub @kumquatfelafel ohh okay! thank you
CamperBot
@camperbot
Jul 23 2017 20:30
adittyagi sends brownie points to @moigithub and @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:warning: adittyagi already gave kumquatfelafel points
:star2: 3526 | @moigithub |http://www.freecodecamp.com/moigithub
Adit
@adittyagi
Jul 23 2017 20:32
Good night guys!
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:32
@adittyagi In this case, the optimization is pretty slight, but if you're doing something pretty intensive inside your for loop, not having to go through that extra iteration can make a difference (so it's good practice to try and notice these things)
night
Adit
@adittyagi
Jul 23 2017 20:34
ok I'll take care of those things!
kumquatfelafel
@kumquatfelafel
Jul 23 2017 20:34
don't... drive yourself crazy... but it's good to occasionally look back on what you did and say, "can this be improved" :p
Adit
@adittyagi
Jul 23 2017 20:35
haha yeah!
Kelechi Chinaka
@ke1echi
Jul 23 2017 20:54
hi @kumquatfelafel
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 21:14

what is wrong with my code var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 5;
};

// Only change code below this line.

var myCar = new Car();
myCar.blazer = "hellfire";

Stanley Su
@stanley-su
Jul 23 2017 21:17
@Astewart400 what are you trying to do?
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 21:17

In the editor, use the Car constructor to create a new instance and assign it to myCar.

Then give myCar a nickname property with a string value.

Joel Y.
@zapcannon99
Jul 23 2017 21:18
@Astewart400 Ignoring my stupid answer from before, Everything seems to check out. Is there a test that's failing?
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 21:18
yea the last on the challenege
Joel Y.
@zapcannon99
Jul 23 2017 21:18
can you send the link
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 21:18
says it needs to be a string but it obviously is
Stanley Su
@stanley-su
Jul 23 2017 21:18
@Astewart400 you said to give it a nickname property. Shouldn't it be myCar.nickName = "hellfire";?
Joel Y.
@zapcannon99
Jul 23 2017 21:19
What Stanley said is right
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 21:19
bruh..
XD
Joel Y.
@zapcannon99
Jul 23 2017 21:19
lol
And here I though blazer was another name for nickname of car >__>
I feel dimb
Can't spell either right now merp
Antonious Stewart
@Antonious-Stewart
Jul 23 2017 21:20
@stanley-su @zapcannon99 thanks
CamperBot
@camperbot
Jul 23 2017 21:20
astewart400 sends brownie points to @stanley-su and @zapcannon99 :sparkles: :thumbsup: :sparkles:
:cookie: 74 | @stanley-su |http://www.freecodecamp.com/stanley-su
:cookie: 312 | @zapcannon99 |http://www.freecodecamp.com/zapcannon99
kumquatfelafel
@kumquatfelafel
Jul 23 2017 21:25
@kelechy hi. Not truly here at moment. But hi, temporarily. :p
FlashHero
@FlashHero
Jul 23 2017 21:38
anyone who can help me with nodejs
kumquatfelafel
@kumquatfelafel
Jul 23 2017 21:40
Off again.
:wave:
Moisés Man
@moigithub
Jul 23 2017 21:45
@FlashHero if u ask ur question maybe some1 who knows will answer :) ... or maybe not :P
Guderian Raborg
@hypercuber
Jul 23 2017 21:48
What is wrong with my code:
https://projecteuler.net/problem=8
let str =
`73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450`;

let u = String.fromCharCode(10);

let arr = str.split('').filter(s => s !== u).map(s => Number(s));

Array.prototype.prod = function(a, b) { //from index a to b, including b
  return this.slice(a, b + 1).reduce((a,b) => a * b); //i to end-1
}

function nDigitsMaxProduct(n) {
  let max = 1;
  for (let i = 0; i < arr.length - n; i++) {
    if (max < arr.prod(i, i + n)) max = arr.prod(i, i + n);
  }
  return max;
}

console.log(nDigitsMaxProduct(13)); // 70573265280 wrong answer
eeflores
@eeflores
Jul 23 2017 21:50
is that a backtick for str?
FlashHero
@FlashHero
Jul 23 2017 21:54
does app.post actually render the content of the file passed in as argument or is it just a string that has to be same as form's action attribute
Guderian Raborg
@hypercuber
Jul 23 2017 22:03
@eeflores yes
eeflores
@eeflores
Jul 23 2017 22:14
@hypercuber do you know what the expected answer is?
Guderian Raborg
@hypercuber
Jul 23 2017 22:15
@eeflores It is 23514624000
Moisés Man
@moigithub
Jul 23 2017 22:17
why ?
eeflores
@eeflores
Jul 23 2017 22:18
@hypercuber I fiddled with the slice, removed the +1 and got your answer
@hypercuber I don't think that's right though
@hypercuber actually the change I did to slice is correct
when you do the +1 you're actually multiplying 14 numbers
Adam Faraj
@adamfaraj
Jul 23 2017 22:24
@adamfaraj
hello
i'm trying to access an objects keys/properties
but it keeps saying undefined
https://codepen.io/adamfaraj/pen/projLp
console.log(data[0].station); does not work
console.log(data) however will give me the object
Guderian Raborg
@hypercuber
Jul 23 2017 22:26
@eeflores I thought slice did not include the second number
Moisés Man
@moigithub
Jul 23 2017 22:27
@adamfaraj data is NOT an array
eeflores
@eeflores
Jul 23 2017 22:28
@hypercuber console.log([1,2,3,4,5].slice(0, 3));
or better still console.log([0,1,2,3,4,5].slice(0, 3));
Adam Faraj
@adamfaraj
Jul 23 2017 22:28
@moigithub gotcha!!
thanks! @moigithub
CamperBot
@camperbot
Jul 23 2017 22:28
adamfaraj sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3527 | @moigithub |http://www.freecodecamp.com/moigithub
eeflores
@eeflores
Jul 23 2017 22:30
@hypercuber so essentially you want 13 elements from the array. when you use slice(0, 13) you'll get an array with elements from 0 to 12
but that's 13 elements
It's Monday morning for me, so my description ... explanation may not be the best
Guderian Raborg
@hypercuber
Jul 23 2017 22:33
@eeflores It is okay, you help me a lot. I had to check the day in my computer to make sure it wasnt Monday for me if not it means I skipped work lol
eeflores
@eeflores
Jul 23 2017 22:34
:thumbsup:
Simon Cordova
@gbsimon87
Jul 23 2017 22:43
Hey all, good evening!
eeflores
@eeflores
Jul 23 2017 22:47
hello @gbsimon87 :wave:
Simon Cordova
@gbsimon87
Jul 23 2017 22:50
Hello! It's been a while since I've been here, I miss the community :)
dyon3334
@dyon3334
Jul 23 2017 23:00
@gbsimon87 welcome back
Simon Cordova
@gbsimon87
Jul 23 2017 23:02
Thanks guys :)
I used to be here all the time, once (largely thanks to this platform) I got my first programming job about 8 months ago I barely find time anymore.
But one can never forget where they came from!
dyon3334
@dyon3334
Jul 23 2017 23:05
@gbsimon87 and do you like your job ? How long did you study brfo
Before
monkeyfingerz
@monkeyfingerz
Jul 23 2017 23:14
hey code camp

function smallestCommons(arr) {
  var range=[];
  var answer = 0;
  var sortedArr= arr.sort(function(a,b){
    return a-b;
  });
  for(var i = 0; i< sortedArr[1]; i++){
    range.push(i);
  }
  var x=2;
  while(answer ===0){
  for(var j = range[0]; j<range.length ; j++){

    if((largestPrime(sortedArr[1])*x)%range[j] ===0){
      answer=x;
         }else{
           j=range[0];
           x++;

         }
  }
      }
   return largestPrime(sortedArr[1])*x;
}
function largestPrime(num) {
  // function to check if the number presented is prime
 var range = [];
  var i =num;
 while(range.length ===0){
   if(isPrimeNumber(i)){
     range.push(i);
   }
   i--;
 }
  return range[range.length-1];
}
function isPrimeNumber(num){
  for( var j= 2 ; j < num ; j++){
    if(num % j === 0){
      return false;
    }
  }
  return true;
}

smallestCommons([23,18]) ;
I'm trying to find theSmallest Common Multiple on the intermediate challenges but this code isn't passing all the tests
I'm sorry if this is hard to read or understand. i'll write notes and repost
OH i figured out the problem
i'm trying to find the largest prime number in one of my functions but one of the test cases doesn't have a prime number in the range
shoooooooot
Nick Cleary
@Hijerboa
Jul 23 2017 23:21
So I'm trying to test my twitch API, and have it call the names off all the streamers that are offline. However, the code which displays the streamers which are in an array labeled offline is called before the code which pushes the offline streamers to that array finishes executing, despite the fact that I call it after the code which pushes the names to an array. Can someone tell me how I would go about delaying this code?
var streamers = [
    "ESL_SC2",
    "OgamingSC2",
    "cretetion",
    "freecodecamp",
    "storbeck",
    "habathcx",
    "RobotCaleb",
    "noobs2ninjas"
];
var streamURL = "https://wind-bow.glitch.me/twitch-api/streams/";
var usersURL = "https://wind-bow.glitch.me/twitch-api/users/";
var online = [];
var offline = [];

function getStatus() {
    for (let i = 0; i < streamers.length; i++) {
        $.getJSON(streamURL + streamers[i], function(status) {
            if (status.stream === null) {
                console.log(streamers[i] + " is currently offline.");
                offline.push(streamers[i]);
                console.log(offline);
            } else if (status.stream.stream_type == "live") {
                console.log(streamers[i] + " is currently streaming.");
                online.push(streamers[i]);
                console.log(online);
            }
        });
    }
}
function findOffline() {
    if (offline.length > 0) {
        console.log("it works");
    } else if (offline.length == 0) {
        console.log("it doesn't work");
    }
}

$(document).ready(function() {
    getStatus();
    findOffline();
});
Marcus Parsons
@marcusparsons
Jul 23 2017 23:34
@Hijerboa, Hey Nick, what you want to do is make an initial call to your $.getJSON call and then call it each time there's a response, regardless of what that response is. You wouldn't want to loop through the streamers array because the loop is going to finish before there is a response back from the API. Here's a codepen I set up using your code.
https://codepen.io/marcusparsons/pen/rzNeOe
Stephen James
@sjames1958gm
Jul 23 2017 23:46
@Hijerboa If you want to launch the requests in parallel, you will need to process the responses in the callbacks. You could push the values to the arrays, and then
when the sum of the lengths of the two arrays is streamers.length then you can process the response arrays
Moisés Man
@moigithub
Jul 23 2017 23:48
another way is using ajaxStop @Hijerboa
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:49
Capture.PNG
@sjames1958gm can you explain this nonsense?
I did nas[0][3] += 1
and it worked fine
but in this instance it returns NaN
Stephen James
@sjames1958gm
Jul 23 2017 23:50
@Giveback007 Are you trying to add to unitialized values?
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:50
@sjames1958gm you can see that they are set to 0 initialy
the first log is the initial state second log is after the conditional += 1
Stephen James
@sjames1958gm
Jul 23 2017 23:51
@Giveback007 no, don't see that, I don't know what score array contains.
If score[i][j] is 3 it looks like you add one to unitialized value
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:52
score array contains values between 0 to 8
Nick Cleary
@Hijerboa
Jul 23 2017 23:53
@marcusparsons so basically call it once as a function and use recursion to recall it?
and unfortunatly that doesn't actually fix the issue :(
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:53
@sjames1958gm what does uninitialized mean?
Moisés Man
@moigithub
Jul 23 2017 23:54
if nas array is empty
nas[][j] will be undefined
undefined +1 ==> NaN
Stephen James
@sjames1958gm
Jul 23 2017 23:54
@Giveback007 You haven't given it a value
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:54
but I did
i loged it on the right
it's filled with 0
or am I missing something?
Moisés Man
@moigithub
Jul 23 2017 23:54
nas is not filled...
nas[i] = [] on ur code
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:55
@moigithub ahhh i re asigned it to []
ahhhH!!
@sjames1958gm @moigithub tnx
CamperBot
@camperbot
Jul 23 2017 23:55
giveback007 sends brownie points to @sjames1958gm and @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3528 | @moigithub |http://www.freecodecamp.com/moigithub
:star2: 8143 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:57
right under my nose too
Moisés Man
@moigithub
Jul 23 2017 23:57
what that code should do ?
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:58
I'm implementing extra features to the GOL
Moisés Man
@moigithub
Jul 23 2017 23:58
rotate values ?? like
if 0 then .. make it 1
if 1 make it 2
if 2 .. make it 3
if 3.. make it 0
Dovydas Stirpeika
@Giveback007
Jul 23 2017 23:59
no score has a value of 0 to 8