Reputation: 35
I want the function to return all 3-digit integers to the front of the array. When I console log this, I get 8 arrays, none of which is the output I'm looking for. I suspect the error starts when I begin using for ().
Also, any tips on how to further clean up the code is appreciated as I am learning best practices as I go.
let numList = [1, 324,34, 3434, 304, 2929, 23, 444]
function checkLength (num){
num.forEach(function (n){
var stringLength = n.toString().length; //n = 324, 204, 444
for (i=0; i<num.length; i++){
if (stringLength == 3){
let a = num.splice (i,1);
num.unshift (a[0]);
}
}
})
console.log(num);
}
checkLength(numList);
Upvotes: 1
Views: 62
Reputation: 39322
Instead of converting number to string you can just check the range. Once a 3 digit number is found, we will remove it from the array using splice()
and add it on front of the array using .unshift()
.
let numList = [1, 324, 34, 3434, 304, 2929, 23, 444];
function checkLength(num) {
num.forEach((v, i, a) => {
if(v > 99 && v < 1000) {
a.unshift(...a.splice(i, 1));
}
});
return num;
}
console.log(checkLength(numList));
Upvotes: 0
Reputation: 191936
Reduce the array into an array of 3 digits, and array of the rest, then flatten them to a single array by spreading into Array.concat()
:
function checkLength(array) {
return [].concat(...array.reduce((r, n) => {
r[n > 99 && n < 1000 ? 0 : 1].push(n);
return r;
}, [[], []]));
}
console.log(checkLength([1, 324, 34, 3434, 304, 2929, 23, 444]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 172
Please have a look at the code. I hope that it is self-explanatory to you. Basically, you have 2 options here:
I'm using the first approach in this code below:
function shiftThreeDigitValuesInFrontOfArray(array)
{
var threeDigitValue; // Will hold the value later,
array.forEach(function(element, index)
{
// We are only interested in 3 digit values
if(element.toString().length === 3)
{
// Remove the value from the array
threeDigitValue = array.splice(index, 1);
// And put it in front of the array
array.unshift(threeDigitValue[0]);
}
});
}
var array = [1, 324,34, 3434, 304, 2929, 23, 444];
// Let the magic begin!
shiftThreeDigitValuesInFrontOfArray(array);
// View the results
console.log(array);
Please let me know if this works for you :)
Upvotes: 0
Reputation: 41
Why do you use a nested loop?
You can do it like this:
var arr = [1, 324,34, 3434, 304, 2929, 23, 444];
function checkLength (arr){
arr.forEach(function (n, index){
if (n >= 100 && n < 1000) {
arr.unshift(arr.splice(index, 1)[0]);
}
});
return arr;
}
checkLength(arr);
Please note that modifying an array you are looping over can be dangerous and might result in something you would not expect.
Upvotes: 0
Reputation: 386520
Basically you need an index of the first items of length with three and another index for iterating the array.
If an item has the string lenght of three splice this item and put it back to the stored index. Then increment that index.
Proceed with the loop.
This proposal keeps the order of the items. It just mooves the items with a string length to to while maintaining the order of these items as well.
function checkLength(array) {
var i = 0,
j;
for (j = 0; j < array.length; j++) {
if (array[j].toString().length === 3) {
array.splice(i++, 0, array.splice(j, 1)[0]);
}
}
return array;
}
console.log(checkLength([1, 324, 34, 3434, 304, 2929, 23, 444]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0
Reputation: 7923
I think you are confused with how Shift works...
The shift() method removes the first element from an array and returns that removed element
It wont bring to the front all 3-digit integers, It will only remove the first element from the array and that is whats gonna be returned.
I think you are looking either sorting or filtering the array.
If you want to sort the array by the length of its value this is a way to do it: .sort(a=>a.toString().length!=3?1:-1)
let numList = [1, 324,34, 3434, 304, 2929, 23, 444]
console.log(numList.sort(a=>a.toString().length!=3?1:-1))
If you want to Filter is almost the same:
let numList = [1, 324,34, 3434, 304, 2929, 23, 444]
console.log(numList.filter(a=>a.toString().length==3?1:0))
Upvotes: 0