r/learnjavascript • • May 08 '26

Is this a useful Array Method I created on accident? or is it useless?

/*********************************************************************
 * Array.pushByVal(value)
 * -----------------------
 * 
 * this only works with integer values.
 *
 * hold on let me cook...
 *
 * if the value is 4 it pushes the number 4 
 * 
 * three times so the total of 4's in the array is 4.
 *
 * if the value is 8 or 9 or whatever, it 
 *
 * pushes the value - 1 that many times.
*********************************************************************/
 
Array.prototype.pushByVal = function(value){
    // what the absolute fuck am i doing?
    // how does this even work?
    for(let i = 0; i < this.length; i++){
        if(this[i] === value){
            return;
        }else{
            this.push(value);
        }
    }
};

let arr = [1, 2, 3, 4, 5];
arr.pushByVal(4);

// Output: [1, 2, 3, 4, 5, 4, 4, 4]
0 Upvotes

58 comments sorted by

View all comments

Show parent comment

5

u/code_monkey_001 May 08 '26

Yes, given the same inputs the same result should occur, but your description is inaccurate and demonstrates a fundamental misunderstanding of what is happening - it doesn't add the number 4 three times because the input is 4. Here's what happens on each pass:

  1. is 1 equal to 4? no, so 4 is pushed, resulting in [1,2,3,4,5,4]
  2. is 2 equal to 4? no, so 4 is pushed, resulting in [1,2,3,4,5,4,4]
  3. is 3 equal to 4? no, so 4 is pushed, resulting in [1,2,3,4,5,4,4,4]
  4. is 4 equal to 4? yes, so processing stops. Final output [1,2,3,4,5,4,4,4]

if your starting array is [4,5,6,7,8,9] and you pass in 4, this happens:

  1. Is 4 equal to 4? yes, so processing stops. Final output [4,5,6,7,8,9]

-1

u/woofmeowmeowwoof May 08 '26

you see how it outputs 2 twice. its the same with any number, any array size. whatever number i pass into pushByVal, that number will be in the array that many times

-1

u/woofmeowmeowwoof May 08 '26

you might be somewhat right. I had 7 push 9 times for some reason.

5

u/code_monkey_001 May 08 '26

Not somewhat right. I explained in simple, step-by-step terms how it works and I'm entirely right. You're just coincidentally right in some of your test cases, and can't explain why.

4

u/HarryBolsac May 08 '26

This comment chain throws me back when i was grinding leetcode type of exercises, thinking i got the exercise right and then it fails like 50% of the tests lmao

1

u/woofmeowmeowwoof May 08 '26

you were wrong. its not that serious. you are not coding jesus. my algs f*ck

5

u/code_monkey_001 May 09 '26

Explain to me how I'm wrong using any of the examples I gave you. My explanation applies to all combinations of numbers, yours only apply to a tiny subset of possible combinations because it's only coincidentally right under very tightly constrained conditions (an array of sequential numbers starting with 1 and including the number you've passed in). Try starting your arrays with 0 and suddenly you'll see very different behavior.

-2

u/woofmeowmeowwoof May 08 '26

i dont want to disagree with you but i can film it with obs to prove you wrong if you want. i am messing with it right now.

let arr = [1, 2, 3, 4, 5];

arr.pushByVal(2);

arr

(6) [1, 2, 3, 4, 5, 2]

3

u/code_monkey_001 May 08 '26

Now do the same starting array [1,2,3,4,5] but call arr.pushByVal(6). For you to be right, it would output [1,2,3,4,5,6,6,6,6,6]. If I'm right, it will output [1,2,3,4,5]. You're just selecting test cases that are coincidentally working like you think.

0

u/woofmeowmeowwoof May 08 '26

it cant do that if 6 is not in the array

6

u/code_monkey_001 May 09 '26

[6] outputs [6]
[6,7,8] outputs [6,7,8]
[6,1,2,3,4,5] outputs [6,1,2,3,4,5]

You're so wrong you can't even understand a rational explanation of how you're wrong.

0

u/woofmeowmeowwoof May 08 '26

let arr = [1, 2, 3, 4, 5, 6];

arr.pushByVal(6);

arr

(11) [1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6]

0

u/woofmeowmeowwoof May 08 '26

but when 6 is there is works fine

0

u/woofmeowmeowwoof May 08 '26

let arr = [1, 2, 3, 4, 5, 6];

arr.pushByVal(5);

arr

(10) [1, 2, 3, 4, 5, 6, 5, 5, 5, 5]

0

u/woofmeowmeowwoof May 08 '26

get fked sir.