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?
i hope my question has become clear enough
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
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;}))```
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; });```
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.