codebyte-intersection-challenge code example

Example: codebyte-intersection-challenge

function FindIntersection(){
//1) Grab the first and second elements to be compared
  let firstString = strArr[0]
  let secondString = strArr[1]

//2) Create empty arrays to store elements, after converted from strings to numbers
  let firstElementArray = []
  let secondElementArray = []

//3) split() a string into an array of substrings
//4) map() calls the provided function once for each element in an array,
//in order, to iterate over each string element you want to covert to a
//number data type, and push to your array
//5) wrap each string element with Number(), to transform from string 
//data type to number data type
  firstString.split(',').map((oneNumber) => {
    firstElementArray.push(Number(oneNumber))
  })
//6) build the same function for the next element in the array of strings
  secondString.split(',').map((oneNumber) => {
    secondElementArray.push(Number(oneNumber))
  })

//7) create a variable to store list of numbers, called myAnswer
//8) use filter(), which creates an array filled with all array elements that pass a test
//9) create a test inside the filter function which uses includes(),
//which determines whether an array contains a specified element.
//Basically, is my secondElementArray element(e) included in my
//firstElementArray element(e) when compared?
//10) Wrap your returned answer inside a toString() method, 
//which returns a string with all the array values, separated by commas

let myAnswer = (secondElementArray.filter(

e => firstElementArray.includes(e))

).toString()

//11) Check to find if numbers are there, if not, return false
  if(!myAnswer){
    return false
  } else  {
    return myAnswer; 
  }
}

Tags:

Misc Example