Filter function thread

Filter function thread
0

#1

Tell us what’s happening:

Hey, why is this replace not working?

Your code so far


function pairElement(str) {
 var arr=str.split("");
 //Split param string to array
 arr.map((elem)=>{
   if(arr[elem]==="G"){
var mediator=arr[elem].replace(elem, [elem, "C"]);
   } else {
var mediatorred=arr[elem].replace(elem, [elem, "G"]);
   }})
 
//For each elem of array, replace string (letter), with an array of the letter at index 0 and the pair letter at index 1 of the nested array

  return arr;
}

pairElement("GCG");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing/


#2

I would suggest something simpler:

function pairElement(str) {
  var paired = []; // array to be returned
  var letters = str.split(''); // split the string to letters
  letters.forEach(letter => { // for each letter in letters
    switch(letter) { // add DNA Pairs
      case 'A':
        paired.push(['A', 'T']);
        break;
      case 'T':
        paired.push(['T', 'A']);
        break;
      case 'C':
        paired.push(['C', 'G']);
        break;
      case 'G':
        paired.push(['G', 'C']);
        break;
    }
  });

  return paired;
}


#3

Thanks, will go through this soon when I have time, but why does my replace stattement solution not work? I know the code is incomplete for passing the challenge as you need A & T solutions as well, but why does the replace part throw an error in the console?


#4

because arr[elem] is undefined for your case. What you are trying to do is arr[index] your elem is not the index value but letter themselves.

Try

      var arr=str.split("");
 //Split param string to array
 arr.map((elem)=>{ console.log(elem)}

and you will understand that elem is not a number but the letter in your arr.


#5

You already split the string into an array of characters. If you use map, you’re saying “run a given function on every element in ab array”. You don’t need to look anything up when you’re inside that function: the parameter you’ve called elem in your code is the character.


#6

Thank you for this, I used the switch statement as a hint and solved it myself, it really helped!!

I do have a weakspot for higher order functions (map,filter,sort,reduce). Does anyone have a good resource for those?


#7

filter

map

sort

reduce


#8

Been there a few times for higher order functions but can’t seem to understand them (yet). Anything else?


#9

If you are not understanding a specific example, post it here and we will attempt to walk you through what is going on.


#10

Alright, thank you, to start with:

Syntax
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
Parameters
callback
Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise. It accepts three arguments:

Is callback replacable with any other name for the function? And what does callback mean? Am I supposed to use the paramaters of the callback function in the code block to be executed in the function or the actual name of the callback function or what is correct way of thinking about callback?


#11

And if so, could you say that filter is: taking every element of an array and for the elements that the callback function returns true, pushes them to a new array and returns that array?


#12

It is just a function. Typically it is an anonymous function which is written inside the filter or it could be a named function which you could reference here.

Yes, you seem to understand.


#13

And what does callback mean? Am I supposed to use the paramaters of the callback function in the code block to be executed in the function or the actual name of the callback function or what is correct way of thinking about callback?


#14

“callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.”

Does this mean elements in the array have to not be undefined or null?


#15

It just represents a function (named or unnamed).

I think an example would help here. Below are two ways to implement the filter method on the same array which creates the same result.

// anonymous function
var array = [1, 2, 3, 4, 5];
var filteredArray = array.filter(function(num) {
  return num >= 4;
});
console.log(filteredArray); // displays [4, 5 ]

OR

// named function
function greaterOrEqualTo4(num) {
  return num >= 4;
});
var array = [1, 2, 3, 4, 5];
var filteredArray = array.filter(greaterOrEqualto4);
console.log(filteredArray); // displays [4, 5 ]

#16

var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

Do the rest of the paramaters have to be nested within the first parameter?


#17

No, I can still filter undefined or null values in an array like:

var array = [1, undefined, 3, null, 5];
var filteredArray = array.filter(function(num) {
  return num === undefined || num === null;
});
console.log(filteredArray); // displays [undefined, null]

What it means by “not invoked for indexes which have been deleted or which have never been assigned values” is arrays which look like the following:

[1, , 3, 4, , 6] // those blank elements between two commas would be ignored

#18

Aha, so what would you call those elemnts? Just blank`?
Why is it called callback though? Does it act like any other function??

Appreciate the help, really really do.


#19

I believe the term is empty element.

You can create them by deleting an element out of an array like in Chrome’s console

var array = [1, 2, 3, , 5];
delete array[1];
console.log(array); // displays [1, empty, 3, empty, 5]

#20

It is just a function which gets passed as an argument to a function. In this case the main function is the filter method which gets pass another function (the callback function).