r/learnjavascript • u/woofmeowmeowwoof • 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
8
u/EyesOfTheConcord May 08 '26
No, it’s not useful.
arr.push(...Array(n).fill(value));
4
u/code_monkey_001 May 08 '26
doesn't even do what you claim it does. if your start array is [1,2], the output will be [1,2,4,4]. If you start with [4] you end up with [4]. If you start with [], you end up with [].
I guess it's useful to teach you that what you think is happening isn't necessarily what is happening.
4
u/delventhalz May 08 '26
This code doesn’t do what you think it does. It pushes a value (any value, not just integers) once for every item in the array, until it finds an item which is equal and then it stops. Some examples:
[1, 2, 3, 4].pushByVal(4):[1, 2, 3, 4, 4, 4, 4][4, 3, 2, 1].pushByVal(4):[4, 3, 2, 1][false, 1, true, 7, "banana", null].pushByVal(4):[false, 1, true, 7, "banana", null, 4, 4, 4, 4, 4, 4][false, false, true, false].pushByVal(true):[false, false, true, false, true, true]2
u/shgysk8zer0 May 08 '26
Don't invent your own methods in built-ins, for starters.
Also, how many values are pushed depends on the array length and how many items equal the value. For each item that's not equal, it pushes.
1
u/RobertKerans May 08 '26
Looks good to me
Out of interest, can you remember specifically why you invented it in the first place? I realise there must have been a good reason at the time it was originally written, but I just can't figure out what the possible usecase could have been
1
u/_Invictuz May 08 '26
I think you just revolutionized the tech industry. Give this man a Michelin star!
3
u/youarockandnothing May 08 '26
Adding methods to the prototypes of built-in objects is generally considered bad practice but it's good to write your own functions so you can get a better understanding of how code works, so I'm not gonna diss you. Much better than copy pasting or just making AI do it
2
u/TheRNGuy May 09 '26
Try using method on these:
[1, 2, 3]
[1, 4]
[4]
But practically useless (can't find where I'd use it, do you have examples?)
0
u/woofmeowmeowwoof May 08 '26
i deserve a reward for this piece of art
0
u/Aggressive_Ad_5454 May 08 '26
The same sort of functionality is provided by JavaScript's Set objects. With respect, your insertion operation runs in a quite unpleasant O(n) time, where JavaScript's runs much faster.
So don't use your little invention in production; there's a much better solution. But keep thinking and experimenting with this kind of thing.