Intermediate Algorithm Scripting - Drop it

Tell us what’s happening:
im incredibly lost here… HOW DO I ACCESS THE FUNCTION FOR ITS USE

function dropElements(arr, func) {
  var args = arr.slice.call(arguments)
  console.log(args[1])
  return args;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Drop it

Link to the challenge:

The function is one of the arguments. You call it with func() like you would any other function.

So my new question is why doesnt this work for any other than the first case?

function dropElements(arr, func) {
  
  for(let i = 0; i < arr.length; i += 1){
    let n = arr[i]
    if(func(n)!== true){
      arr.pop()

    }
  }
  return arr;
}

console.log(dropElements([1, 2, 3], function(n) {return n < 3; }))
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}))
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))

Mutating an array as you iterate over it produces confusing results.

1 Like

so ive been trying … am im just not sure what to try… or where to “go” from here… i should say i very specifically dont want any kind of answer… im pretty sure from how ive been engaging this problem that ig … my brain seems pretty sure that there is a chance of me really getting a deeper understanding of JS’s “mechanics” from this problem… but frustration and if im being honest in my plea for help here… Impostor Syndrome, seems to be winning out here… suggestions?
i dunno what to ask exactly but ive included one of the “problem outputs” as a console.log for context

function dropElements(arr, func) {
  let newArr = arr.slice()
  
  const args = arr.slice.call(arguments)
  for(let i = 0; i < newArr.length; i += 1){
    let n = newArr[i]
    if(func(n)=== true){
      arr = newArr.slice(0, arr.indexOf(n))

    }
  }
  return arr;
}

console.log(dropElements([1, 2, 3], function(n) {return n < 3; }))
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))

ps… ik my code isnt done… i just cant really see what im building here or something… i dunno hoestly … but its buggin me craaazy
i see why the second call of the function isnt working… i just dont quite see what to do about it
like do i i instigate a second style of iteration … if so… how or more accurately… when and then maybe … how? :confused:
i hope my question has become clear enough :slight_smile:

I’d focus here. What do you want to happen the very first time you find an array element that makes the function func return true? Do you need to keep looping at that point?

Tell us what’s happening:
ive tried a variety of builds and i keep solving all but 1 or 2 of the test cases
commented out was a differentn build for the true circumstance , but it filtered out all false cases not just false cases before a true one and if im understanding the problem correctly…
if the arguement function is calling for number less than 3
and the array is [0, 1, 2, 3 ]
then the return should be
[0, 1, 2, 3 ]
correct?

as i understand it I am NOT dropping anything after a true statement

Your code so far

function dropElements(arr, func) {
  let newArr = arr.slice()
  let result = []
  console.log("Init" , arr)
  for(let i = 0; i < newArr.length; i += 1){
    let n = newArr[i]

    if(func(n) === true){
      //let str = arr.join(" ")
      //let item = str.charAt(str.indexOf(n))
      //item = Number(item)
      //result.push(item)
      arr= arr.slice(n)
      return arr
      
    }
    
    if(func(n)!== true){
      arr.pop()
      console.log(arr)
    }
    
  }

  return arr;
}

console.log("1st",dropElements([1, 2, 3], function(n) {return n < 3; }))
console.log("2nd" , dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Drop it

Link to the challenge:

Why this part?

This challenge is much, much harder if you make any changes to arr inside of your loop. I don’t think you need this part at all.

i see you, my question is commented in my updated code so far

function dropElements(arr, func) {
  let newArr = arr.slice()
  
  console.log("Init" , arr)
  for(let i = 0; i < newArr.length; i += 1){
    let n = newArr[i]

    if(func(n) === true){
     console.log(n)
     // why doesnt it include 1(for 1st) 3(for 2nd) in 
     //the slice
      arr= arr.slice(n)
     
      return arr
      
    }
    
    
    
  }

  return arr;
}

console.log("1st",dropElements([1, 2, 3], function(n) {return n < 3; }))
console.log("2nd" , dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}))

Do you want to slice on n? slice takes an index as it’s first argument.

1 Like

I have a train to catch but i have this thing i just tried that just DIDNT work… Im gonna include it in the hopes you might have some more clarity for me to glean off of (thank you btw) by the time im home again

function dropElements(arr, func) {
  let newArr = arr.slice()
  
  console.log("Init" , arr)
  for(let i = 0; i < newArr.length; i += 1){
    let n = newArr[i]
     let str = arr.join(" ")
     let item = str.charAt(str.indexOf(n))
      console.log("item" , item)

    if(func(n) === true){
     console.log(n)
     
     
      console.log("index",str.indexOf(n))
      arr= arr.slice(str.indexOf(n))
     
      return arr
      
    }
    
    
    
  }

  return arr;
}

console.log("1st",dropElements([1, 2, 3], function(n) {return n < 3; }))
console.log("2nd" , dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}))```

You don’t need to go looking for the index of n in the array, you got n out of the array by using it’s index.

im a tad lost still… even if it is extra steps

), why isnt it working??

Why isn’t which ‘it’ working?

The extra steps are getting in your way here. You are adding bugs by doing stuff that you do not need.

Let’s back up and start from ground zero

  1. never change arr

  2. don’t copy arr

  3. find the first index i such that arr[i] makes the func true

Can you try to do just that part?

function dropElements(arr, func) {
  for(let i = 0; i < arr.length; i += 1){
    let n = arr[i];
    if (func(n)){
      console.log(n)
    }

  }

  return arr;
}

dropElements([1, 2, 3], function(n) {return n < 3; });```

Right. Now why are you creating the variable n? I think it’s confusing you.

You are finding the first element that makes func true instead of the first index i such that arr[i] makes func true

because i prefer it to typing arr[i]… its a general practice i have with for loops…


why should i not

You can do it if you don’t use i and n interchangeably. But you are confusing i and n. That’s a danger with a variable name like n that doesn’t tell you that it means.

If you use words as variable names, then your code reads more like pesudocode or English.

omg i is the index n is the value …

1 Like