These are chat archives for FreeCodeCamp/HelpJavaScript

12th
Aug 2018
Fikry82
@Fikry82
Aug 12 2018 02:29
function updateRecords(id, prop, value) {
  if(prop != "tracks"  && value != "" && !collection[id].hasOwnProperty(prop)){
     collection[id][prop] = value;
  }else if(prop == "tracks" && collection[id].hasOwnProperty("tracks")==false && value !=""){
var myArr =[value];
     collection[id]["tracks"] = myArr;
  }else if(prop=="tracks" && collection[id]["tracks"]){
collection[id]["tracks"].push(value);
  }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
i wanna know why when call function with value = "" rest the property value ??
any help
(2548, "artist", ""), artist should not be set
Fikry82
@Fikry82
Aug 12 2018 02:36
?
Fikry82
@Fikry82
Aug 12 2018 02:59
?
Brad
@bradtaniguchi
Aug 12 2018 05:40
@Fikry82 not sure what your saying your issue is. Also using "2-equals" instead of three could result in odd behavior in code. Always use 3 equals (===) or if your negating it use just two (!==)
Patrick star and Sponge bob
@Joshua-leyer
Aug 12 2018 09:21
hello !
var myStr = "This is the first sentence. ";
myStr += "This is the second sentence.";
Why is there no space at the end?
Raghav Mundra
@Raghav17
Aug 12 2018 10:13
Hi everyone, Can anybody please tell me how to use this 'login' element of first object?
https://api.github.com/users/raghav17/following
ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 10:21
Hey guys I am doing the Sorted Union Algorithm and my first step is to get all the arrays passed to be put all in one array. However this is where I'm stuck and this is my code:
function uniteUnique(arr) {
  arr2 = [];
  arr.forEach( (elem) => arr2.push(elem))
  return arr2;

}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
When you log arr it only returns the first array [1,3,2]
Greg Duncan
@GregatGit
Aug 12 2018 10:30
@ArchivalBoat50 when you use forEach - i don't think you should be using push. First just put them into one then a simple sort
ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 10:33
@GregatGit How do I put them all into one array? Thats where I'm currently stuck.
Aditya
@ezioda004
Aug 12 2018 10:35
@ArchivalBoat50 You can use rest parameter(ES6 syntax) or arguments object.
function uniteUnique(...arr) {
   console.log(arr); //will return all the arrays passed as arguments
}
ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 10:37
@ezioda004 Ohhh thanks, that was exactly what I needed
Ozarion
@Ozarion
Aug 12 2018 19:10
Hey everyone.
I'm on Apis and Microservices Projects - Timestamp Microservice
And I've completed the project. What is it asking me to put in Github Link input field?
Tiago Correia
@tiagocorreiaalmeida
Aug 12 2018 19:17
it expects you to have the code on a github repository that you can link so later one people can see it
Ozarion
@Ozarion
Aug 12 2018 19:19
@tiagocorreiaalmeida Oh.. okay. And One more thing, I just pasted the links and it says challenge completed. Why doesn't it test? Am I missing something?
Tiago Correia
@tiagocorreiaalmeida
Aug 12 2018 19:27
There's no tests on this type of projects for the moment you paste the link and the repository and if for some reason someone see's you have something that's not considered valid they can remove you the certificates
Ozarion
@Ozarion
Aug 12 2018 19:29
Okay. Cool.
Brad
@bradtaniguchi
Aug 12 2018 20:00
definantly more of a learning thing, dont "cheating yourself" and not do parts of the project haha
Nathaniel Suchy
@nsuchy
Aug 12 2018 20:02

I am doing some coding challenges and came across a bug in my Javascript Program. I can run all test cases without an issue EXCEPT when b is equal to 0. The script takes two numbers a and b as input. It runs a for loop to create an array of all numbers between a and b, it uses b to determine when to stop adding numbers to the array. This approach fails when b is equal to 0. What alternate approach could I take to squash this bug?

Source: https://gitlab.com/snippets/1743808

Brad
@bradtaniguchi
Aug 12 2018 20:05
you can check if b is equal to or less than zero before running your existing code. Or you can take into account if a === b too, since Id assume that could result in a bug aswell (by making a quick high-level overlook of your code)
Aditya
@ezioda004
Aug 12 2018 20:06
@nsuchy Why not take relative difference to determine when to stop adding numbers to the array. Math.abs(b-a), you'll get the range that way
Nathaniel Suchy
@nsuchy
Aug 12 2018 20:07
@bradtaniguchi Thanks for the quick response sometimes a will be equal to -1 while b is equal to 0. Even if we check if b is equal to 0 is still has to be checked and handled another way.
@ezioda004 Hmm, is that in the Javascript standard library?
Aditya
@ezioda004
Aug 12 2018 20:09
@nsuchy Yes .abs() method is available in the Math object which is in the global object.
Nathaniel Suchy
@nsuchy
Aug 12 2018 20:10
@ezioda004 So j < Math.abs(b-a)?
Aditya
@ezioda004
Aug 12 2018 20:11
@nsuchy Yep, if you want you can also write your own, something like this
const range = (a, b) => b < a ? a - b : b - a;
Nathaniel Suchy
@nsuchy
Aug 12 2018 20:15
@ezioda004 So the function range outputs an integer with the amount of numbers between a and b?
Aditya
@ezioda004
Aug 12 2018 20:16
@nsuchy Yes,
range(1, 2) // 1
range(0, -1) // 1
range(10, -2) // 12
Nathaniel Suchy
@nsuchy
Aug 12 2018 20:17
interesting bug happens, probably my code but now the test cases all fail :(
Screen Shot 2018-08-12 at 4.17.34 PM.png
Aditya
@ezioda004
Aug 12 2018 20:23
Its because how you've implemented this loop
  for (let i = 0; j < range; i++) {
    let myVal = a + i;
    numberArray.push(myVal);
    numberArray[i] > 0 ? j++ : null
  }
Pagnito
@Pagnito
Aug 12 2018 20:26
im deplying to heroku
my build succeeds for some reason
Screen Shot 2018-08-09 at 3.49.47 PM.png
my build bundles arent served properly
Screen Shot 2018-08-12 at 4.25.45 PM.png
disregard first image
Nathaniel Suchy
@nsuchy
Aug 12 2018 20:27
@ezioda004 Should I get rid of the loop entirely and try something different?
Aditya
@ezioda004
Aug 12 2018 20:28

@nsuchy You should rethink this logic

let myVal = a + i;
    numberArray.push(myVal);
    numberArray[i] > 0 ? j++ : null

What if a < b and so on. A better (and I think simpler) approach to create array would be looping through starting from the min of (a, b) till the max of (a, b) and simply push those elements in b/w to an array

tundeiness
@tundeiness
Aug 12 2018 20:41
hello guys. I'm having challenges changing the position of element #main-cover please. I am using flexbox and I want the element to move to the right, and I want the navbar to have a fixed position. https://codepen.io/highness/pen/bjjzJj
aarivalagan
@aarivalagan
Aug 12 2018 20:52

I am new to Angular5

  • I am trying to submit three different forms which are in same web page and gets stored in firebase database under different child nodes
  • These form records values related to courses and uploading new field values over time
  • I am trying append the fields of one form with other form field values in the same child node but no idea of executing it.
  • for example: in one form field op1 is present and in other form op1 is present, I am trying to append both values from the two forms, forming an array under single child node
  • I have provided entire relevant code for the issue and can you guys let me know how to append the values of one data field to other
  • kindly provide your suggestion
  • gist link: ad.component.html: https://gist.github.com/aarivalagan/ac15e8e2c6f77d0687c01a70e18bca6b

ad:component.ts: https://gist.github.com/aarivalagan/a9c1d22c1d6056da624f0968fb6cd59c

vData.service.ts: https://gist.github.com/aarivalagan/8bfbe47ef8cf0dac267374a8f0ef5b0f

constructor(private db: AngularFireDatabase, private router: Router, private fb: FormBuilder, private dataService: VDataService) {
this.createForm();
this.QForm();
this.DForm();
this.items = this.db.list('/newCour');
this.items1 = this.db.list('/newCour1');
this.items2 = this.db.list('/newCour2');
}
publish() {
alert('Pusblished');
this.item.key = this.vForm.value.name;
this.item.an1 = this.vForm.value.an1;
this.item.Com = this.vForm.value.Com;
this.item.isActive = true;
this.item.isQCom = false;
this.item.Score = 0;
this.item.name = this.vForm.value.name;
this.item.URL = this.vForm.value.URL;
this.item.em = this.vForm.value.em;
this.item.Opa = this.vForm.value.Opa;
this.item.Opb = this.vForm.value.Optb;
this.item.Opc = this.vForm.value.Optc;
this.item.Opd = this.vForm.value.Optd;
this.item.q1 = this.vForm.value.q1;
this.item.file = this.vForm.value.file;
this.test.push(this.item);
this.db.list(/newCourse/).push(this.vForm.value);
console.log(this.vForm.value.name);
}

publishQ() {
    alert('Pusblished');
    this.item1.key = this.Q_Form.value.name;
    this.item1.answer1 = this.Q_Form.value.an1;
    // this.item.isActive = true;
    this.item1.isQCom = false;
    this.item1.Score = 0;
    this.item1.cour_name = this.Q_Form.value.name;
    this.item1.q_name = this.Q_Form.value.q_name;
    this.item1.Opa = this.Q_Form.value.Opta;
    this.item1.Opb = this.Q_Form.value.Optb;
    this.item1.Opc = this.Q_Form.value.Optc;
    this.item1.Opd = this.Q_Form.value.Optd;
    this.item1.q1 = this.Q_Form.value.q1;
    this.test1.push(this.item1);
    this.db.list(`/newCourse1/`).push(this.Q_Form.value);
    console.log(this.Q_Form.value.name);
}

publishD() {
    alert('Pusblished');
    this.item.key = this.D_Form.value.name;
    this.item.file = this.D_Form.value.file;
    this.test2.push(this.item);
   // this.items.push(this.test);
    this.db.list(`/newCour2/`).push(this.D_Form.value);
    console.log(this.D_Form.value.name);
}
ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 20:53

Hey guys I am doing the Sum All Odd Fibonacci Numbers algorithm and I come across Maximum call stack size exceeded
My code seems to be working though.

const sumFibs = (num) => {
    const numbers = [1];
    const fibCalc = (nextNumber = 1, lastNumber = 0) => {
        const newNum = nextNumber + lastNumber;

        numbers.push(newNum);

        if (numbers.length !== num) {
            return fibCalc(newNum, nextNumber);
        }
    }

    fibCalc();
    // console.log(numbers);
    return numbers.reduce( (accumulator, value ) => value%2 === 1 && value<num ? accumulator+value : accumulator+0);
}

sumFibs(75024);

How is this happening, is it happening because I am processing too many numbers in the code?
How can I fix this?

ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 20:58
I'm guessing I need to find a different way to get the fibonacci array
Moisés Man
@moigithub
Aug 12 2018 21:02
the challenges says.. generate n fib nums.. until ur last generated fibnum is > the value they provide @ArchivalBoat50
soo fibs(100) should generate only 10 or 20 fib nums... not 100
ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 21:30

@moigithub Thanks I fixed my code a little now and got all the checkmarks but one sumFibs(75025) should return 135721.

My code:

const sumFibs = (num) => {
    const numbers = [1];
    const fibCalc = (nextNumber = 1, lastNumber = 0) => {
        const newNum = nextNumber + lastNumber;

        numbers.push(newNum);

        if (numbers.length !== num && newNum <= num) {
            return fibCalc(newNum, nextNumber);
        }
    }

    fibCalc();
     console.log(numbers);
    return numbers.reduce( (accumulator, value ) => value%2 === 1 && value<num ? accumulator+value : accumulator+0);
}

sumFibs(75025);

sumFibs(75025) is returning 60696. I notice that the code is not adding the odd number in the array: 75025 at the second last index. I can't understand why this is

Moisés Man
@moigithub
Aug 12 2018 21:43
u can remove this && value<num from ur reduce
(or use && value <=num )
...but that extra check isnt needed i think.. cuz u already did when generating values @ArchivalBoat50
ArchivalBoat50
@ArchivalBoat50
Aug 12 2018 21:46
image.png
Oops
Accidently Pressed the prtscrn button
But anyway when I removed && value<=num i lost a lot of checkmarks. However using &&value<=num it worked. Why is that?
When i removed && value <num **
@moigithub
Moisés Man
@moigithub
Aug 12 2018 21:56
probably cuz ur generator still generating too much values...
remve this numbers.length !== num && @ArchivalBoat50
Fikry82
@Fikry82
Aug 12 2018 23:13
function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){
    if(contacts[i].hasOwnProperty(prop) && contacts[i].firstName === name){
             return  contacts[i][prop];
    }else if(!contacts[i].hasOwnProperty(prop)){
             return "No such property";
    }else if(contacts[i].firstName != name){
             return "No such contact";
    }
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
not working with all cases
any help
Fikry82
@Fikry82
Aug 12 2018 23:18
?
Christopher McCormack
@cmccormack
Aug 12 2018 23:22
@Fikry82 hello - you're returning in every conditional. For example, if the name does not match, you should continue to the next loop, but instead you are returning No such contact
Fikry82
@Fikry82
Aug 12 2018 23:45
okay i used string to hold the value and return it on time at the end but still not working
Moisés Man
@moigithub
Aug 12 2018 23:53
probably u should re-post ur updated code.... @Fikry82
Fikry82
@Fikry82
Aug 12 2018 23:53
okay
function lookUpProfile(name, prop){
// Only change code below this line
var myString = "";
for(var i = 0; i < contacts.length; i++){
    if(contacts[i].hasOwnProperty(prop) && contacts[i].firstName === name){
          myString = contacts[i][prop];
    }else if(!contacts[i].hasOwnProperty(prop)){
              myString ="No such property";
    }else if(contacts[i].firstName != name){
           myString = "No such contact";
    }

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

// Change these values to test your function
lookUpProfile("Akira", "likes");
i have problem now when prop = likes
Christopher McCormack
@cmccormack
Aug 12 2018 23:56
@Fikry82 there are some conditionals that you do want to return right away, it's not all or nothing.