I’m trying to insert “num” into “arr” with the para function but I can’t do it arr[i].[1] = unaFuncionqueGuarda
; do you have any ideas? if anyone could help me figure out how to do it i would really appreciate it!
function getIndexToIns(arr, num) {
var unaFuncionqueGuarda = [arr, num].slice (1);
for (var i = 0; i < arr.length; i++){
arr[i].[1] = unaFuncionqueGuarda
}
console.log (arr);
return num;
}
getIndexToIns([40, 60], 50);
beiti
April 30, 2021, 4:08pm
2
Hi Pablo,
I am not sure why you are making it so complicated?
There is a function called “splice” that will do exactly what you want:
function insertNumberIntoArray(arr, num, pos) {
arr.splice(pos, 0, num);
}
insertNumberIntoArray([10, 12, 14, 16], 13, 2);
You need to know the position where you want to insert the number.
If you must use your code - there are a few mistakes.
You are returning num
- why? That is one of the parameters you have defined.
arr[i].[1]
doesn’t work. If you want to access the first element of a nested array, do it without the .
- like this:
let array = [ [1, 2, 3] , [10, 11, 12] ] ;
array[1][1] // refers to 11.
Do you know why it refers to 11? If not, let me know, I could guide you to the proper tutorials.
What are you doing with the first var unaFunctionqueGuarda
? It would return num
inside an array the way you are using it. Is that what you want?
I hope I could help!
All the best, and good health,
Sebastian.
1 Like
Really aprecciate it! splice is working a bit but idk why it refers to 11. Can you lead me to those tutorials?
beiti
April 30, 2021, 6:24pm
4
Hi Pablo,
most welcome.
A general introduction to JavaScript is here:
And if you already know most of it and just want to dive into the question of arrays, then start at this position:
And feel free to ask anytime!
All the best and good health,
Sebastian.
1 Like
system
Closed
October 30, 2021, 6:37am
6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.