These are chat archives for FreeCodeCamp/HelpJavaScript

30th
Mar 2018
Conor Murphy
@cmurphy580
Mar 30 2018 00:40

i'm doing a react project where i make over an array of objects that is used to create a component. i'm getting this error and i'm not sure as to why:

Uncaught (in promise) TypeError: news.map is not a function

anyone have any ideas or come across a similar issue?

brianna4444
@brianna4444
Mar 30 2018 01:05
Hi guys, I need help with the JS intermediate algorithm challenges for front end cert. Really lost and its too much to post on these chats for help. Looking to pay someone to tutor me remotely. Please message me if you'd be interested.
Stephen James
@sjames1958gm
Mar 30 2018 01:12
@cmurphy580 Are you making sure the initial value of news is [] ?
Greg Duncan
@GregatGit
Mar 30 2018 02:40
@cmurphy580 if you console.log the value of map before you see it's not an array - whenever you get this error it just means the variable (in this case news) isn't an array
Joshua Frias
@Juke-Magic
Mar 30 2018 06:27
https://codepen.io/Juke-Magic/pen/jZVEJa?editors=1010
guys why is data.stream undefined?
Markus Kiili
@Masd925
Mar 30 2018 10:54
@Juke-Magic All those getJSON callbacks use the same i variable that is Players.length when those callbacks execute at some point later (after the loop and the script have finished executing).
So Players[i] should be undefined.
1Mher1
@1Mher1
Mar 30 2018 11:45
Hello who can help me JavaScript Arrays?
Matej Bošnjak
@mbosnjak01
Mar 30 2018 11:51
you can just ask the question
somebody will eventually get you an answer @1Mher1
1Mher1
@1Mher1
Mar 30 2018 11:52

var zuyg =[1,2,3,4,5]
var sum=[]
for(x=0;x<zuyg.length;x++){

if (zuyg[x]%2==0){

    console.log(zuyg[x])
}

}

in this code I must summon pairs this example pairs 2 and 4 I found already but i must do that pairs i must summon 2+4=6
please help me summon
Fabien SHAN
@X140hu4
Mar 30 2018 11:59
@1Mher1 Do you have the link to the challenge?
Have you tried with the hints?
1Mher1
@1Mher1
Mar 30 2018 12:01
tried other option to solve this but i can't freecodecamp chaat said me only this chat can help me
1Mher1
@1Mher1
Mar 30 2018 12:08
@X140hu4 can you help me?
Ian
@toianw
Mar 30 2018 12:10
@1Mher1 You need to sum all the even numbers, right?
1Mher1
@1Mher1
Mar 30 2018 12:11
@toianw only pairs number
no all in array
Ian
@toianw
Mar 30 2018 12:12
like 2, 4, 6, 8, 10? @1Mher1
1Mher1
@1Mher1
Mar 30 2018 12:12
yes
Ian
@toianw
Mar 30 2018 12:13
ok, so I would initialize sum to 0 var sum = 0;
Tiago Correia
@tiagocorreiaalmeida
Mar 30 2018 12:14
hello everyone
1Mher1
@1Mher1
Mar 30 2018 12:14
but in above message check my array [1,2,3,4,5] I must print 6 because my pairs number is 2 and 4 2+4=6
@tiagocorreiaalmeida Hi
Ian
@toianw
Mar 30 2018 12:16
then you can add each even number to sum in the loop. When the loop has finished you can console.log the value of sum
btw 2, 4, 6, 8, ... are called even numbers in English.
1Mher1
@1Mher1
Mar 30 2018 12:22
I don't understand your explained code please write here
Ian
@toianw
Mar 30 2018 12:25
Something like this:
var zuyg = [1, 2, 3, 4, 5];
var sum = 0;
for (var x = 0; x < zuyg.length; x++) {
  if (zuyg[x] % 2 === 0) {
    sum += zuyg[x];
  }
}

console.log(sum);
1Mher1
@1Mher1
Mar 30 2018 12:28
@toianw same numbers The arithmetic average what can I find?
Marianissimus
@Marianissimus
Mar 30 2018 12:29
the arithmetic average is something very different from what you said first, @1Mher1
it would mean: add ALL the numbers to the sum var, then divide the sum by zuyg.length;
you don't need to check any condition any more (like zuyg[z] % 2 === 0)...
1Mher1
@1Mher1
Mar 30 2018 12:34
I will print in console.log The arithmetic average finally numbers exact 4 and 5 because my The arithmetic average in my array is [1,2,3,4,5] (1+2+3+4+5)/5 = 3 exact i will Print an array of elements that are larger than the arithmetic mean of the array elements
Marianissimus
@Marianissimus
Mar 30 2018 12:35
i understand almost nothing of what you say. what is this challenge? send a link
1Mher1
@1Mher1
Mar 30 2018 12:36
link is foreign languages

Print an array of elements that are larger than the arithmetic mean of the array elements

For example, in the case of [1,2,3,4,5] the mean arithmetic will be (1 + 2 + 3 + 4 + 5) / 5 = 3 so it should print 4 and 5

Stephen James
@sjames1958gm
Mar 30 2018 12:39
@1Mher1 First you will need to calculate the mean then the new array. Two loops would be required
1Mher1
@1Mher1
Mar 30 2018 12:40
yes now that
and done this
Stephen James
@sjames1958gm
Mar 30 2018 12:41
@1Mher1 Do you have code that does this? can you show it?
1Mher1
@1Mher1
Mar 30 2018 12:43
for ( var i = 0; i < zuyg.length; i ++){
var avg = (zuyg[i] / zuyg.length) * zuyg.length
}
console.log(avg)
var zuyg = [1, 2, 3, 4, 5]
for ( var i = 0; i < zuyg.length; i ++){
var avg = (zuyg[i] / zuyg.length) * zuyg.length
}
console.log(avg)
Stephen James
@sjames1958gm
Mar 30 2018 12:45
@1Mher1 You are not adding up the values of the array. You have to do the division after you sum all the values.
This code will add each value to sum (after the loop sum will be 1 + 2 + 3 + 4 + 5)
var zuyg = [1, 2, 3, 4, 5];
var sum = 0;
for (var x = 0; x < zuyg.length; x++) {
    sum += zuyg[x];
}
1Mher1
@1Mher1
Mar 30 2018 12:47
in my code above I shown I want to do this (1+2+3+4+5)/5 = 3
Stephen James
@sjames1958gm
Mar 30 2018 12:48
@1Mher1 Your code will only result in
var avg = (5 / 5) * 5;
Because you set avg to a new value every time
Marianissimus
@Marianissimus
Mar 30 2018 12:48
@1Mher1 here, I like to help :)
function aboveAverage(arr){
    let avg =  ( arr.reduce( (acc, curr) => acc + curr) ) / arr.length;
    return arr.filter(el => el > avg);
}
Stephen James
@sjames1958gm
Mar 30 2018 12:49
@Marianissimus Really? They are having trouble with basic loops and you throw all that out?
1Mher1
@1Mher1
Mar 30 2018 12:50
@Marianissimus thx but first I don't use function return arr.filter
CamperBot
@camperbot
Mar 30 2018 12:50
1mher1 sends brownie points to @marianissimus :sparkles: :thumbsup: :sparkles:
:cookie: 484 | @marianissimus |http://www.freecodecamp.org/marianissimus
1Mher1
@1Mher1
Mar 30 2018 12:54
@sjames1958gm var total = 0;
for(var i = 0; i < zuyg.length; i++) {
total += zuyg[i];
}
var avg = total / zuyg.length;
console.log(avg)
I found the arithmetic average only want to solve this 4 and 5
Stephen James
@sjames1958gm
Mar 30 2018 12:58
@1Mher1 So another loop that compares zuyg[i] to avg
1Mher1
@1Mher1
Mar 30 2018 13:00
if(zuyg[i]<=3){}
????
Stephen James
@sjames1958gm
Mar 30 2018 13:01
@1Mher1 Compare to avg not 3
@1Mher1 You'll need to build an array
var newArr = [];
// Loop that adds to newArr if > avg
console.log(newArr)
1Mher1
@1Mher1
Mar 30 2018 13:04
namely i must have empty array for example newArr than i write if(newArr<avg){than write what?}
Stephen James
@sjames1958gm
Mar 30 2018 13:05
@1Mher1 not newArr > avg but (zuyg[i] > avg)
How do you add to an array?
1Mher1
@1Mher1
Mar 30 2018 13:07
my all code this var zuyg = [1, 2, 3, 4, 5]
var total = 0 for(var i = 0; i < zuyg.length; i++) {
total += zuyg[i]
}
var avg = total / zuyg.length
console.log(avg)
please help me to change this and write correct
Stephen James
@sjames1958gm
Mar 30 2018 13:07
@1Mher1 Now you need another loop
var newArr = [];
for(var i = 0; i < zuyg.length; i++) {
   if (zuyg[i] > avg) {
      // add zuyg[i] to newArr
   }
}
console.log(newArr);
1Mher1
@1Mher1
Mar 30 2018 13:19
but they print empty array
in console.log(newArr)
Ian
@toianw
Mar 30 2018 13:31
@1Mher1 You need to write one more line of code where Stephen put the comment // add zuyg[i] to newArr
1Mher1
@1Mher1
Mar 30 2018 13:32
yes I put my idea option but they print empty array\
Ian
@toianw
Mar 30 2018 13:34
You can use push to add new items to an array.
What did you try?
1Mher1
@1Mher1
Mar 30 2018 13:38
@toianw finally code can you write me I don't understand
var newArr=[]
var total = 0
for(var i = 0; i < zuyg.length; i++) {
total += zuyg[i]
if(zuyg[i] > avg){}
}
var avg = total / zuyg.length
console.log(avg)
in cycle I tried to change and give new arr but they print empty array
Ian
@toianw
Mar 30 2018 13:45
You need two different loops. So first put those parts together:
// Loop one finds the average:
var zuyg = [1, 2, 3, 4, 5];
var total = 0; 

for (var i = 0; i < zuyg.length; i++) {
  total += zuyg[i];
}

var avg = total / zuyg.length;
console.log(avg); // 3

// Loop two filters the array:
var newArr = [];
for(var i = 0; i < zuyg.length; i++) {
   if (zuyg[i] > avg) {
      // add zuyg[i] to newArr
   }
}
console.log(newArr);
Then, where you see this comment (// add zuyg[i] to newArr) you just need to add one line of code to push zuyg[i] to newArr.
Stephen James
@sjames1958gm
Mar 30 2018 14:03
@1Mher1 I left something for you to do adding the value to newArr do you know how to add to an array?
Pjdaze
@Pjdaze
Mar 30 2018 14:19
ho guys i'm stuck on this challenge, i'm supposed to set a default parameter so that if there is no value i return number + 1

const increment = (function() {

  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
Marianissimus
@Marianissimus
Mar 30 2018 14:22
@Pjdaze I guess you could try to change the return like this: return number + value || number+1;
Pjdaze
@Pjdaze
Mar 30 2018 14:23
no lol just got it! lol
const increment = (function() {
  "use strict";
  return function increment(number, value = number + 1) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
Marianissimus
@Marianissimus
Mar 30 2018 14:23
but can I ask why are you declaring a self-invoking function?
Pjdaze
@Pjdaze
Mar 30 2018 14:23
is a challeneg they made it this way lol wasnt me
all i had to do was set number as one of the argumetns
then comma then value = number + 1
i was only doing value = number + 1
and actually its still wrong lol but getting there heheh
Marianissimus
@Marianissimus
Mar 30 2018 14:25
but what you just gave doesnt work...
i don't think you can write an assignment in the parameters, can you?
Pjdaze
@Pjdaze
Mar 30 2018 14:27
function greeting(name = "Anonymous") {
  return "Hello " + name;
}
console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello Anonymous
that works just fine...
i dont know why my challenge makes it a self inv function
Marianissimus
@Marianissimus
Mar 30 2018 14:28
oh, i see. good to find that out
Pjdaze
@Pjdaze
Mar 30 2018 14:29
const increment = (function() {
  "use strict";
  return function increment(number, value = 1) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
ok that does work ! try it hehehe
Marianissimus
@Marianissimus
Mar 30 2018 14:29
but that's a default parameter value, so yeah, it should be value = 1;
also use strict is from them? part of the homework?
Pjdaze
@Pjdaze
Mar 30 2018 14:29
i forgot i was already returning number + value so all i had to do was set value = 1 as the default param
yes use strict is there
ahh love js haha
Marianissimus
@Marianissimus
Mar 30 2018 14:30
interesting. I never used default values for parameters before; could save a lot of conditionals...
Pjdaze
@Pjdaze
Mar 30 2018 14:30
i know right
have u tried beta.freecodecamp.com
is glitchy but has a lot of awesome es6 lessons
Marianissimus
@Marianissimus
Mar 30 2018 14:31
is it different from the freecodecamp?
Pjdaze
@Pjdaze
Mar 30 2018 14:31
yes!!!
i know mind blowing hehe
Marianissimus
@Marianissimus
Mar 30 2018 14:32
i'll try it, but... not quite now, i'm wasting a lot of time as is:)
Pjdaze
@Pjdaze
Mar 30 2018 14:32
some one just told me and i looked it up, so far at lest for me, i can log in so it doesnt save my progress
Marianissimus
@Marianissimus
Mar 30 2018 14:34
yeah, we need to switch to for of loops and arrow functions; have you read this: https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
NikolaNbgd
@NikolaNbgd
Mar 30 2018 14:48
Hello Folks. Does anyone of you every used google scripts for html for submit? I wanna when user submit form, to receive an email user data and to get those data in my google spread sheet. I got email, but my google spread sheet is empty. Any help, advice? Thanks in advance!
Nate Mallison
@NJM8
Mar 30 2018 14:53
Hey I'm looking for some advice on how to correctly debounce a $(document).on('scroll') function. I have a debounce function, I am just rotating a down arrow to be an up arrow after the user scrolls down a specific amount, the problem is after debouncing it if the user scrolls up to the top too fast the arrow doesn't rotate back.
www.natethedev.com is the site.
the arrow there now is not debounced, but I am concerned that will make it slow for some users. Of course I'm only animating one element so maybe I'm being overly concerned
1Mher1
@1Mher1
Mar 30 2018 16:16
@sjames1958gm no please help me
Stephen James
@sjames1958gm
Mar 30 2018 16:16
Use push function
1Mher1
@1Mher1
Mar 30 2018 16:17
I done that but they printed console.log 3,4

var total = 0;
for (var i = 0; i < zuyg.length; i++) {
total += zuyg[i];
}

var avg = total / zuyg.length;
console.log(avg)

var newArr = [];
for(var i = 0; i < zuyg.length; i++) {
if (zuyg[i] > avg) {
newArr.push(i)
}
}
console.log(newArr);

Stephen James
@sjames1958gm
Mar 30 2018 16:19
Push the value from the array
1Mher1
@1Mher1
Mar 30 2018 16:22
@sjames1958gm thank you so much
CamperBot
@camperbot
Mar 30 2018 16:22
1mher1 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 9149 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
1Mher1
@1Mher1
Mar 30 2018 16:23
this chellange finally question ask and stoped

@sjames1958gm Announce X digital array

Get a new Y array containing the elements in the X array located in [10; 20]

1Mher1
@1Mher1
Mar 30 2018 16:37

first step I done already var foo = [];

for (var i = 0; i <= 20; i++) {
foo.push(i);
}
console.log(foo)

this step please help Get a new Y array containing the elements in the X array located in [10; 20]
anybody who can help me this
Stephen James
@sjames1958gm
Mar 30 2018 17:08
@1Mher1 You aren't using array X? And why start at 0 instead of 10?
1Mher1
@1Mher1
Mar 30 2018 17:12
X I was changed foo
Stephen James
@sjames1958gm
Mar 30 2018 17:13
@1Mher1 The instructions have X the input and Y the output you only have one array
1Mher1
@1Mher1
Mar 30 2018 17:15

@sjames1958gm var foo = []
var y = []
for (var i = 0; i <= 20; i++) {
foo.push(i)
if(i==10){

y.push(i)
}
}
console.log(foo)
console.log(y)

but again don't print what chellange want
Stephen James
@sjames1958gm
Mar 30 2018 17:33
@1Mher1 Don't you need an X array with at least 20 elements?
You push the elements not the index.
Tiago Correia
@tiagocorreiaalmeida
Mar 30 2018 19:17
hello everyone
today I readed an article that said this was a bad pratice
const app = express(),
    port = process.env.port || 3000;
declare multiple variables using the , instead of define every with the const keyword
but doesnt makes really sense to me?
Alexander Køpke
@alexanderkopke
Mar 30 2018 19:21
did it say why it was bad practice? I've seen it done many time in udemy courses so makes no sense to me that anyone would teach others to do it, if it is bad practice.
Christopher McCormack
@cmccormack
Mar 30 2018 19:40
One problem would be that you may forget a comma or add an extra comma and that could cause errors. It doesn't really save any time to use the comma format

Looking at the actual style guide it may also have to do with

Declared when needed, initialized as soon as possible

which would mean variables might be moved around more often. Comma format would make that more difficult, again for no gain.

Tiago Correia
@tiagocorreiaalmeida
Mar 30 2018 19:47
yeah same here @alexanderkopke
and yeah @cmccormack that's the one :D
Alexander Køpke
@alexanderkopke
Mar 30 2018 19:50
No gain? It makes you look cool and hip! :laughing:
Tiago Correia
@tiagocorreiaalmeida
Mar 30 2018 19:52
ahahaha :D
anyone into a codefights tournament?
Christopher McCormack
@cmccormack
Mar 30 2018 20:51
@tiagocorreiaalmeida never even looked into it, fun?
Ayush Bahuguna
@relentless-coder
Mar 30 2018 20:59
Need some help with React. So I am trying the new Context API, but the browser console throws error, 'createContext' is not a function
export const {Provider, Consumer} = React.createContext(null);
Christopher McCormack
@cmccormack
Mar 30 2018 21:02
@relentless-coder what version?
Tiago Correia
@tiagocorreiaalmeida
Mar 30 2018 21:48
@cmccormack still around? yeah can be called fun :D we can give it a tr