I am trying to find the negative
value in an array, once found I would like to break and exit the loop and print the results. While using shift()
it is printing only upto 3 iteration, could someone please advise about the issue here.
let newArr = [100,200,0,400,500,-50,600];
function findNegValue(newArr) {
for(let i =0; i <= newArr.length ; i ){
let arrayValue = newArr.shift();
console.log("EachArrayValues::" arrayValue);
if(arrayValue < 0){
console.log("Negative value found, ie " arrayValue);
break;
} else {
continue ;
}
}
}
findNegValue(newArr);
//Output:
EachArrayValues::100
EachArrayValues::200
EachArrayValues::0
EachArrayValues::400
// Without shift() it is working fine
let newArr = [100,200,0,400,500,-50,600];
function findNegativeNumber(newArr) {
for(let i =0; i < newArr.length ; i ){
let arrayValue = newArr[i];
console.log("len::" newArr.length);
console.log("EachArrayValues::" arrayValue);
if(arrayValue < 0){
console.log("Negative value found, ie " arrayValue);
break;
}
}
}
findNegativeNumber(newArr);
len::7
EachArrayValues::100
len::7
EachArrayValues::200
len::7
EachArrayValues::0
len::7
EachArrayValues::400
len::7
EachArrayValues::500
len::7
EachArrayValues::-50
Negative value found, ie -50
CodePudding user response:
Documentation says:
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
Each iteration you're accessing the current array element and removing the first element. So basically you're skipping half of the elements because each iteration the previous first element was already removed and your i
variable is being incremented.
To fix that access the array element through [i]
operator (as you're already doing) or Array.at
method.
let newArr = [100,200,0,400,500,-50,600];
function findNegativeNumber(newArr) {
for(let i =0; i < newArr.length ; i ){
let arrayValue = newArr[i];
// let arrayValue = newArr.at(i);
// ...
}
}
findNegativeNumber(newArr);
And if you want to use shift
method then do not use i
to control the index, just use shift
with a while
statement:
let newArr = [100,200,0,400,500,-50,600];
function findNegativeNumber(newArr) {
while (newArr.length !== 0) {
let arrayValue = newArr.shift();
// ...
}
}
findNegativeNumber(newArr);
Also there's a shortcut and more elegant solution to find the first negative number of an array:
let newArr = [100,200,0,400,500,-50,600];
let firstNegativeNumber = newArr.find(e => e < 0);
let allNegativeNumbers = newArr.filter(e => e < 0);
CodePudding user response:
Whenever you shift
your array, it is taking the first value out of the array. Hence, i
is not less than newArr.length
at half of the length of the original newArr
.
const myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i <= myArr.length; i ) {
let arrayValue = myArr.shift();
console.log("by unshift::" arrayValue);
console.log("by [i]:: " myArr[i]);
console.log("=======");
}
CodePudding user response:
You can use a while loop to achieve what you are trying to do
let newArr = [100,200,0,400,500,-50,600];
while(newArr.length){
let arrayValue = newArr.shift();
console.log("EachArrayValues::" arrayValue);
if(arrayValue < 0){
console.log("Negative value found, ie " arrayValue);
break;
} else {
continue ;
}
}
Output :
EachArrayValues::100
EachArrayValues::200
EachArrayValues::0
EachArrayValues::400
EachArrayValues::500
EachArrayValues::-50
Negative value found, ie -50