These are chat archives for FreeCodeCamp/HelpJavaScript

14th
Apr 2018
ArchivalBoat50
@ArchivalBoat50
Apr 14 2018 02:48
Hey guys, I'm working on trying to find the longest word in a string, I first split the string into individual words and then for looping through it, but I don't know how to find the longest word at this point:
var str = "I am number four";
var words = str.split(/\s/g);
console.log(words);

for (var i = 0; i < words.length; i++) {

}
Dhaval Vira
@dhavalveera
Apr 14 2018 02:48
Hello World
Brad
@bradtaniguchi
Apr 14 2018 03:57
@dhavalveera Hello
Dhaval Vira
@dhavalveera
Apr 14 2018 03:57
@bradtaniguchi Hello
Brad
@bradtaniguchi
Apr 14 2018 03:57
@ArchivalBoat50 A key insight here is being able todo this: 'myString'.length
length is available on strings, and on arrays. (A string is an array of letters for the most part)
kerafyrm02
@kerafyrm02
Apr 14 2018 06:34
Anyone need help?
Brad
@bradtaniguchi
Apr 14 2018 06:36
@kerafyrm02 I'm still here
Oh wait never mind your asking if people need help haha nvm :{
kerafyrm02
@kerafyrm02
Apr 14 2018 06:36
:P
Not exactly sure what you mean by a string is an array of letters;
Brad
@bradtaniguchi
Apr 14 2018 06:41
@kerafyrm02 At a low level a string is saved as an array of characters in the computer memory. JS abstracts this away so its just a "string" type. But thinking as a string as an array of characters helps understanding some of the features available to strings, such as using length.
Markus Kiili
@Masd925
Apr 14 2018 06:43
@bradtaniguchi It is not just a String type. Strings are primitive values that are wrapped into an array-like object when you access its properties. That is why you can use them as if they were an array of characters.
Brad
@bradtaniguchi
Apr 14 2018 06:44
That is a much better explanation
Simon Cordova
@gbsimon87
Apr 14 2018 07:00
Was wondering if anyone here had any React Native experience?
has had**
Brad
@bradtaniguchi
Apr 14 2018 07:15
Man ive been going so slowly just to test 1 thing XD Bouncing around between all the stuff and just being distracted XD Shoulda taken 15 mins, has taken almost 2 hours haha
abraham anak agung
@padunk
Apr 14 2018 07:16
@bradtaniguchi you should close gitter :smile:
Brad
@bradtaniguchi
Apr 14 2018 07:16
@padunk Yea, but its pretty dead so I think that's the LAST thing that's distracting me XD
abraham anak agung
@padunk
Apr 14 2018 07:19
@bradtaniguchi you know C?
Brad
@bradtaniguchi
Apr 14 2018 07:21
@padunk I used C in the past, never got to deep. I know the basics and the overall process
abraham anak agung
@padunk
Apr 14 2018 07:21
@bradtaniguchi can I PM you ? If you have free time off course.
Brad
@bradtaniguchi
Apr 14 2018 07:21
@padunk Sure todays my chill day haha
abraham anak agung
@padunk
Apr 14 2018 07:44
@bradtaniguchi ty for pm.
CamperBot
@camperbot
Apr 14 2018 07:44
padunk sends brownie points to @bradtaniguchi :sparkles: :thumbsup: :sparkles:
:cookie: 461 | @bradtaniguchi |http://www.freecodecamp.org/bradtaniguchi
Brad
@bradtaniguchi
Apr 14 2018 07:44
@padunk np anytime :D
Long Nguyen
@longnt80
Apr 14 2018 08:20

$('body').click(function(e){ e = $(e.target) $.each(set.handler, function(k,v){ if(e.is(v)) set[k](e) }) })
Anyone knows what this set.handler is?
Markus Kiili
@Masd925
Apr 14 2018 08:23
@longnt80 Must be something defined elsewhere in the code.
1Mher1
@1Mher1
Apr 14 2018 08:44
please help me I want to print in empty array even number than summon

var zuyg = []
var sum = 0
for (var x = 0; x < 10; x++) {
if (zuyg[x] % 2 === 0) {
sum += zuyg[x]
}

}

console.log(sum)

Fabien SHAN
@X140hu4
Apr 14 2018 08:45
What have you tried?
1Mher1
@1Mher1
Apr 14 2018 08:47

@X140hu4 var zuyg = []
var sum = 0
for (var x = 0; x < 10; x++) {
if (zuyg[x] % 2 === 0) {
sum += zuyg[x]
}

}

console.log(sum)

they print 0
Pieter Stokkink
@forkerino
Apr 14 2018 08:48
@1Mher1 you put nothing in the array, so your if is always false
1Mher1
@1Mher1
Apr 14 2018 08:49
I should in empty array
I must done thisin empty array
Pieter Stokkink
@forkerino
Apr 14 2018 08:55
@1Mher1 so you want to put even numbers in the zuyg array?
abraham anak agung
@padunk
Apr 14 2018 10:29
@1Mher1 you can push the even number in the array and use next for loop to sum it
var zuyg = []
var sum = 0
for (var x = 0; x < 10; x++) {
    if (x % 2 === 0) {
        zuyg.push(x);
    }
}
for (var i = 0; i < zuyg.length;i++){
    sum += zuyg[i];
}

console.log(sum)
kerafyrm02
@kerafyrm02
Apr 14 2018 12:19
why push into an array? why not just add it inside 1st loop?
Muhammad Hasham
@MohammadHasham
Apr 14 2018 12:44

can anyone explain me

form: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Form",
            index: true
        },

the type and ref and index property what are they doing here? and are they necessary?

Nate Mallison
@NJM8
Apr 14 2018 15:17
@MohammadHasham Is that the whole thing? can you post the whole schema?
the types prop here means it is storing the object ID, and ref: 'Form' means it's expecting it to be from the form schema
so I have a shopping list app with a users DB and an items DB, users schema:
const userSchema = new mongoose.Schema({
  username: {
  type: String,
  required: true,
  unique: true
  }, 
  password: {
    type: String,
    required: true
  },
  isAdmin: {
    type: Boolean,
    default: false
  },
  items: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Item'
  }]
}, 
  {timestamps: true}
);
items schema
const itemSchema = new mongoose.Schema({
  name: String,
  quantity: Number,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }
},{timestamps: true}
);
Stephen James
@sjames1958gm
Apr 14 2018 15:20
@NJM8 So ref means that it is a reference to another collection?
Nate Mallison
@NJM8
Apr 14 2018 15:21
so each user has an array of items, which is a reference to the "Item" collection and the id is stored
and each item has a ref back to the "User" collection with the id stored
@sjames1958gm yes
Stephen James
@sjames1958gm
Apr 14 2018 15:21
@NJM8 That part I didn't know - thanks
CamperBot
@camperbot
Apr 14 2018 15:21
sjames1958gm sends brownie points to @njm8 :sparkles: :thumbsup: :sparkles:
:cookie: 403 | @njm8 |http://www.freecodecamp.org/njm8
Nate Mallison
@NJM8
Apr 14 2018 15:22
it's a little confusing because under the User collection a document (user) will not have an array like so:
[{type: itemID, ref: 'Item"}];
it will just store the id, mongoose does some magic so that when you store an item to a user it just gets the id
['itemID1', 'itemID2'];
wow, I helped @sjames1958gm , I think my dev career has peeked. haha
403, my most coveted brownie point
Stephen James
@sjames1958gm
Apr 14 2018 15:27
Isn't 403 - forbidden :)
Nate Mallison
@NJM8
Apr 14 2018 15:32
oh, lol
Stephen James
@sjames1958gm
Apr 14 2018 15:41
Glad I could make your day. :p
JJ Megma
@megma_jj_twitter
Apr 14 2018 18:03
@padunk
var sum = 0
for (var x = 0; x < 10; x++) {
    if (x % 2 === 0) {
       sum+=x
    }
}
console.log(sum)
Tiago Correia
@tiagocorreiaalmeida
Apr 14 2018 18:40
hey there
Adrienne Michelson
@AdriMichelson
Apr 14 2018 20:36
exports.csvToJSON = function(file, callback) {
  csvtojson().fromFile(file, function(err, result) {
    if (err) {
      console.log("An Error Has Occured");
      console.log(err);
      return
    }
    console.log(JSON.stringify(result), result.type);
    return result;
    callback(null, result);

  });
  console.log(callback)
}
correctly outputs a json object onto the console, but when I call the function, it returns undefined???
this is node.js
Bjorn van de Peut
@bjorno43
Apr 14 2018 20:45
@AdriMichelson Not entirely sure if it's the issue, but JS is case sensitive: exports.csvToJSON => csvtojson()
Adrienne Michelson
@AdriMichelson
Apr 14 2018 20:59
hmm I will try that ty
Stephen James
@sjames1958gm
Apr 14 2018 21:04
@AdriMichelson You have a return before you invoke the callback.
Also the function itself returns undefined. Since fromFile is asynchronous - return is not useful