These are chat archives for FreeCodeCamp/Help

8th
Mar 2015
Chasecode
@Chasecode
Mar 08 2015 03:51
i have an array question
Yosi
@jparkton
Mar 08 2015 03:51
@Chasecode kgo
Cristián Berríos
@crisberrios
Mar 08 2015 03:51
tell us
Chasecode
@Chasecode
Mar 08 2015 03:51

var prompts = [
'Type your name',
'Type an adjective',
'Type a noun'
];

// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function() {
if (currentPrompt < prompts.length) {
//put current prompt in all html elements with class
$('.prompt').html(prompts[currentPrompt]);
// move the next prompt into variable currentPrompt
currentPrompt = currentPrompt + 1;
}
// else we're at end of array
else {
$('.prompt').html("that's all for now!");
}
}

thats my code
so the prompts.length returns 3....
Yosi
@jparkton
Mar 08 2015 03:52
ok
Chasecode
@Chasecode
Mar 08 2015 03:52
and the currentPrompt starts at 0
Yosi
@jparkton
Mar 08 2015 03:52
yes
oh lol
Cristián Berríos
@crisberrios
Mar 08 2015 03:53
paste code with a line with 3 backticks before and 3 in a line after code ```
Yosi
@jparkton
Mar 08 2015 03:53
I see where you are going
the indexs for an array start at 0
Chasecode
@Chasecode
Mar 08 2015 03:53
so how does the function ever equal 3?
which it clearly does
Yosi
@jparkton
Mar 08 2015 03:54
one sec
Chasecode
@Chasecode
Mar 08 2015 03:54
'''test'''
test
Yosi
@jparkton
Mar 08 2015 03:54
var prompts = [
    'Type your name',
    'Type an adjective',
    'Type a noun'
   ];

// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function() {
  if (currentPrompt < prompts.length) {
    //put current prompt in all html elements with class 
    $('.prompt').html(prompts[currentPrompt]);
    // move the next prompt into variable currentPrompt 
    currentPrompt = currentPrompt + 1;
  }
  // else we're at end of array
  else {
    $('.prompt').html("that's all for now!");
  }
}
Chasecode
@Chasecode
Mar 08 2015 03:54
tyty
Yosi
@jparkton
Mar 08 2015 03:54
use the ` to the left of 1 on qwerty boards
not '''
:)
Chasecode
@Chasecode
Mar 08 2015 03:55
gotcha, ty
Yosi
@jparkton
Mar 08 2015 03:55
Ok so the array,
Cristián Berríos
@crisberrios
Mar 08 2015 03:55
well, the function doesn't reach 3
arrays start at 0
Yosi
@jparkton
Mar 08 2015 03:55
the length of prompts is what?
Cristián Berríos
@crisberrios
Mar 08 2015 03:55
and since array.length is 3
you iterate 3 times before reaching 3
0, 1 and 2
Chasecode
@Chasecode
Mar 08 2015 03:56
so it should never get to the "else" part?
Cristián Berríos
@crisberrios
Mar 08 2015 03:56
it will, after going through 3 iterations
once it reaches 3 it will jump to the else part
Chasecode
@Chasecode
Mar 08 2015 03:57
ohhhh
it doesn't stop at the end of the arrays
it keeps going
Cristián Berríos
@crisberrios
Mar 08 2015 03:58
but that code is still incomplete
Chasecode
@Chasecode
Mar 08 2015 03:58
2+1 = 3
doh
yeah, i have left off the buttion part
Yosi
@jparkton
Mar 08 2015 03:58
glad I could help
er.....
Chasecode
@Chasecode
Mar 08 2015 03:59
tyvm
Suzanne Atkinson
@AdventureBear
Mar 08 2015 06:32
@crisberrios good news...I'm getting the hang of some iterators!
Bad news...I'm not sure why these forEach iterators are pushing an "undefined" to my newArr.
Christian Petersen
@fnky
Mar 08 2015 06:33
for(;;) excited!
Can you supply your code?
Suzanne Atkinson
@AdventureBear
Mar 08 2015 06:33
I "cheated" by using filter (another iterator!). and it passes half the test s in the bonfire
but I think there is something i need to do so I don't have to filter the result. I'm not sure why it's pushing an undefined or null value in each "forEach".
the bonfire is
Chirag Sejpal
@cbsejpal
Mar 08 2015 06:44
Hello guys. Need some help. Codepen.io is not showing me the preview.
Any one of you happen to know what will be the issue?
I'm on Challenge 16
Suzanne Atkinson
@AdventureBear
Mar 08 2015 06:45
can you share your codepen link?
Chirag Sejpal
@cbsejpal
Mar 08 2015 06:49
I'm not able to save it don't know why :(
Carlos Toro Vera
@carmelo12341
Mar 08 2015 06:50
maybe a screenshot
Chirag Sejpal
@cbsejpal
Mar 08 2015 06:51
codepen.PNG
Suzanne Atkinson
@AdventureBear
Mar 08 2015 06:57
not sure. Do you need to include jquery in that one?
Chirag Sejpal
@cbsejpal
Mar 08 2015 06:57
Nope.
But it should at lease show the preview :worried:
Suzanne Atkinson
@AdventureBear
Mar 08 2015 06:58
if you can't save the pen, maybe there is some other issue preventing the preview from rendering?
Chirag Sejpal
@cbsejpal
Mar 08 2015 06:59
Previously I used to see preview without even saving it
Oliver
@Dadsaster
Mar 08 2015 07:33
@AdventureBear The reason you have all those undefined is because after you forEach arr1 it becomes an empty array. When you forEach the arr2 your indexOf and empty array returns undefined which evaluates to -1.
function diff(arr1, arr2) {
  var newArr = [];

  // Same, same; but different.
   arr2.forEach(function(item){
      //console.log(arr1);
      if (arr1.indexOf(arr2[item])=== -1 && arr2[item] !== undefined){
         newArr.push(arr2[item]); 
      }  
      console.log(newArr);
  });


    arr1.forEach(function(item){
      //console.log(arr2);
      if (arr2.indexOf(arr1[item])=== -1 && arr1[item] !== undefined){
         newArr.push(arr1[item]); 
      }  
      console.log(newArr);
  });

    return newArr;
}

diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
You could also iterate through 1 array only and add an else condition that removes the the duplicate items in the second array and then concat them together. This wouldn't produce any undefineds
Briana Swift
@brianamarie
Mar 08 2015 13:25
Hello! Still working on "where art thou" bonfire. Is there a way to access the value of a specefic key within an object without knowing the name of the key? I've found out how to access it if I know the key's name, but the keys are inconsistant through tests.
kian
@meecoder
Mar 08 2015 13:47
@brianamarie maybe you could use [object name].keys[0] to get the first key?
Andreas Bylund
@andreasbylund
Mar 08 2015 14:07
Sometimes when I do the challenges at freecodecamp.com I have to reload the page to be able to press the buttons. Only me who got that problem?
Briana Swift
@brianamarie
Mar 08 2015 14:22
@mecoder that works to get the keys, but I need to reference them later to get the value for the keys.
Oliver
@Dadsaster
Mar 08 2015 14:53
@brianamarie in order to access the keys in an object you can use a for in loop
It looks like this:
for (var key in object) {
    console.log(key);
}
key is a variable and can be named anything. The above code will console every key in the object. If you changed it to object[key] then it would console all the values in object. I hope this helps.
That's the cool thing about objects. You can access key value pairs without knowing the names of the keys.
Briana Swift
@brianamarie
Mar 08 2015 14:56
thank you! :) exactly what I was looking for!
Oliver
@Dadsaster
Mar 08 2015 14:56
Glad I could help
Suzanne Atkinson
@AdventureBear
Mar 08 2015 14:58
@Dadsaster thanks for the tip, I'll need to reread that few times
would creating a new array (a copy) help in preventing the undefinned?
it takes a village! Lord knows @crisberrios & @terakilobyte are trying thier best with me, lol.
Oliver
@Dadsaster
Mar 08 2015 15:00
I don't think so. I think because the arrays aren't the same length you are going to get some undefines.
That's why I suggested iterating only through one array.
Suzanne Atkinson
@AdventureBear
Mar 08 2015 15:01
what if you had something like this though (not sure if any tests are like this for the bonfire).
[1,2,3,4,5], [2,3,4,6]
the output should 1,5,6 right? You'd have to check each array to see if the item was in the other array. You'd get [6] from the first compare and [1,5] from the second compare
am I making it too complicated?
Oliver
@Dadsaster
Mar 08 2015 15:10
The logic I was using was something like - iterate through array1 - if an element is unique keep it. else remove element from both arrays. In your example this would result in creating [1,5] and [6]. Then you just need to concat array 2 onto array 1 and repeat the same process for additional arrays.
There isn't a 'wrong' way - just less or more efficient.
Danny Fritz
@dannyfritz
Mar 08 2015 15:48
i do my diff with a reduce and a filter
Suzanne Atkinson
@AdventureBear
Mar 08 2015 15:49
@dannyfritz can you elaborate a little?
Danny Fritz
@dannyfritz
Mar 08 2015 15:50
i combine all the arguments into an array
then filter and see if they are unique
Oliver
@Dadsaster
Mar 08 2015 16:20
This only works if you first test individual arrays for uniqueness first
otherwise [1,1,2,3][2,3,4] would return [4] instead of [1,4]
Danny Fritz
@dannyfritz
Mar 08 2015 16:22
that's true
Oliver
@Dadsaster
Mar 08 2015 16:22
I do have to learn to use reduce better
Meta Hirschl
@MetaCoderHirschl
Mar 08 2015 18:34
question -- I can't get what seems like a simple bonfire -- convert special characters to the html escape characters. I think the solution is correct but bonfire says no -- any idea why (maybe I'm not understanding the problem): ```
function convert(str) {
  // &colon;&rpar;
  str = str.replace(/&/g, "&amp");
  //str = str.replace(/&/g, "&");

  str1 = str.replace(/>/g, "&gt");

  str1 = str.replace(/</g, "&lt");

  str1 = str.replace(/"/g, "&quot");

  str1 = str.replace(/'/g, "&#039");
  console.log(str1);

  return str1;
}

convert('Dolce & Gabbana');
error is: assert.strictEqual(convert('Dolce & Gabbana'), 'Dolce & Gabbana', 'should escape characters');AssertionError:should escape characters: expected 'Dolce & Gabbana' to equal 'Dolce & Gabbana' -- output is -- Dolce &amp Gabbana -- as expected, yes?
naming confusion (mixed up str1 and str) here it is fixed (but not working...)
function convert(str) {
  // &colon;&rpar;
  str = str.replace(/&/g, "&amp");
  //str = str.replace(/&/g, "&");

  str = str.replace(/>/g, "&gt");

  str = str.replace(/</g, "&lt");

  str = str.replace(/"/g, "&quot");

  str = str.replace(/'/g, "&#039");
  console.log(str);

  return str;
}

convert('Dolce & Gabbana');
returns -- "Dolce &amp Gabbana"
Oliver
@Dadsaster
Mar 08 2015 18:44
in regex certain characters need to be preceeded by a \ in order for the computer to understand what you are asking for. both ' and " need a \
I believe & may need the \ as well - you can google it
I suggest messing around with regex in something like: https://regex101.com/
Meta Hirschl
@MetaCoderHirschl
Mar 08 2015 18:52
thanks for info @Dadsaster -- for this specific answer however, the result is accurate and doesn't pass test. that's what confuses me most. if you put "Dolce &amp Gabbana" in browser it will display exactly as requested, i.e. Dolce & Gabbana
Danny Fritz
@dannyfritz
Mar 08 2015 18:55
aren't you missing a semicolon?
&amp;
Meta Hirschl
@MetaCoderHirschl
Mar 08 2015 18:57
I removed the semicolons...I thought they were errors. oops... thanks @dannyfritz
@dannyfritz passed with semicolons...there I go trying to second guess things. :worried: Those semicolons just looked wrong to me...like I know....
Danny Fritz
@dannyfritz
Mar 08 2015 18:59
:)
Yosi
@jparkton
Mar 08 2015 19:52
always those ;
tricky little boogers
Meta Hirschl
@MetaCoderHirschl
Mar 08 2015 21:22
@jparkton :smile:
Carlos Toro Vera
@carmelo12341
Mar 08 2015 23:23
is anyone here?
kian
@meecoder
Mar 08 2015 23:23
@carmelo12341 I am :)
Carlos Toro Vera
@carmelo12341
Mar 08 2015 23:23
I need help :'c
with the mutations bonfire
kian
@meecoder
Mar 08 2015 23:24
@carmelo12341 well I'm not there yet but maybe I can help
Carlos Toro Vera
@carmelo12341
Mar 08 2015 23:25
it is simple, just check if the second element in a given array is part of the first element
This message was deleted
Cristián Berríos
@crisberrios
Mar 08 2015 23:27
hey @carmelo12341
what is troubling you?
Carlos Toro Vera
@carmelo12341
Mar 08 2015 23:30
I'm not sure if what I'm doing is alright
function mutation(arr) {
    var array1 = arr[0].toLowerCase(),
        array2 = arr[1].toLowerCase();
    // console.log(array1.indexOf(array2) >= 0);
    array1 = array1.replace(/\s/g,"");
    array1 = array1.split("");
    array2 = array2.split("");
    var answer = array2.forEach(function(element, index, array){
        for (var i = 0; i < array1.length; i++) {
            if (element === array1[i]) {
                return true;
            }
        }
        return answer;
    })
}
Cristián Berríos
@crisberrios
Mar 08 2015 23:35
the 1st part looks ok, but the forEach algorithm doesn't look good
remember that forEach just iterates but doesn't return values
Carlos Toro Vera
@carmelo12341
Mar 08 2015 23:36
mmm
Danny Fritz
@dannyfritz
Mar 08 2015 23:36
returning from a forEach will merely shortcircuit the current iteration.
Carlos Toro Vera
@carmelo12341
Mar 08 2015 23:42
after running it I obtain undefined :/
Cristián Berríos
@crisberrios
Mar 08 2015 23:42
look into the other array methods
and check closely what they return
and what expects from the result of each iteration