Reputation: 9
I'm really new to JavaScript and I've been struggling with this code for a test to get into a coding bootcamp. I wonder if anyone had any help they could give?
function putInTheMiddle(array, item) {
for (i = 0; i < 20; i++) {
let Middle = Floor.Math(array / 2);
array.slice(Middle, 0, item);
}
console.log putInTheMiddle([1, 3], 2);
console.log putInTheMiddle([1, 2, 4, 5], 3);
}
I would like it to print out the arrays as [1, 2, 3]
and [1, 2, 3, 4, 5]
respectively.
Upvotes: 0
Views: 804
Reputation: 2420
Since, you are new to javascript i would like to answer this with simple methods.
//array is the array in which you want to insert.
//item is the value which you want to insert.
function putInMiddle(array, item){
let index = Math.floor(array.length/2);
//array.splice is a method which takes 3 arguments.
//argument 1 is the index where you want to make change.
//argument 2 is number of elements you want to remove starting from that index.
//argument 3 is the element that you want to insert at that index.
//In your case no element needs to be removed, hence argument 2 is 0.
array.splice(index,0,item);
return array;
}
//Then you can call the function in console.log if you just want to print the value.
console.log(putInMiddle(['1','2','4','5'],'3'));
//This will print ['1','2','3','4','5']
The above code works perfectly if you just want to add 1 element. But what if you want to add more than one element at a time. Relax, i have got you covered for that. Use the following:
function putInMiddle(array, itemArr){
let index = Math.floor(array.length/2);
let numItems = itemArr.length;
for(let i=0; i<numItems; i++){
array.splice(index,0,itemArr[i]);
index++;
}
return array;
}
console.log(putInMiddle(['1','2','6','7'],['3','4','5']));
//This will print ['1','2','3','4','5','6','7']
Hope this helps!
Upvotes: 0
Reputation: 3820
What the OP posted needed a few corrections which the following code reflects:
function putInTheMiddle(arr, item) {
let m = Math.floor((arr.length / 2));
arr.splice(m, 0, item);
return arr;
}
let arr = putInTheMiddle([1, 3], 2);
console.log(`[` + arr.join() + `]`);
let arr2 = putInTheMiddle([1, 2, 4, 5], 3);
console.log(`[` + arr2.join() + `]`);
The user-defined function needs to return the altered array in each case if console.log is to display the intended result. Note, Math is an object and floor
is a method just as console is an object and log
is a method. The methods require parentheses.
In order to determine the middle index of the array, you need to find its length, easily available since it is a property of the array. After dividing the length in two, you need to round the result to the nearest integer less than or equal to its value and Math.floor() will aid in this endeavor.
Instead of using the slice method, the code utilizes the Array object's splice method which fits the task at hand perfectly.
I concur with @Barmar that the OP does not need the for-loop.
Lastly, I use the Array object's join() method to join the elements into a string for easy display, which does not permanently affect the array.
Upvotes: 0
Reputation: 44125
Use Math.floor
with array.length
- also, console.log
is a function, so call it with parentheses ()
:
function putInTheMiddle(array, item) {
array.splice(Math.floor(array.length / 2), 0, item);
return array;
}
console.log(putInTheMiddle([1, 3], 2));
console.log(putInTheMiddle([1, 2, 4, 5], 3));
.as-console-wrapper { max-height: 100% !important; top: auto; }
Upvotes: 1
Reputation: 22418
Try Math.floor
rather than Floor.Math
:
const putInTheMiddle = (array, item) => {
let middleOfArray = Math.floor(array.length / 2);
array.splice(middleOfArray, 0, item);
return array;
}
console.log(putInTheMiddle([1, 3], 2));
console.log(putInTheMiddle([1, 2, 4, 5], 3));
Upvotes: 1
Reputation: 781751
You don't need the loop, that will insert the number 20 times.
The function to get the floor of a value is Math.floor()
, not Floor.Math()
.
You need to divide the array's length by 2, not the array itself.
The function modifies the array in place, but if you want to be able to call console.log()
when calling the function, it needs to return the array as well.
You need to use the splice()
method to insert into the array. slice()
just extracts part of the array without modifying.
You need to put parentheses around the argument to console.log()
, and this needs to be outside the function definition.
function putInTheMiddle(array, item) {
let Middle = Math.floor(array.length / 2)
array.splice(Middle, 0, item);
return array;
}
console.log(putInTheMiddle([1, 3], 2));
console.log(putInTheMiddle([1, 2, 4, 5], 3));
Upvotes: 1