Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I just try to search data from 2 dimensional array. for that reason I just run 2 for loop. one for index & second one is for row index. my first loop is working, but second one condition isn't work.
var arr = [
[1, 2, 3, 4, 5]
[0, 8, 7, 6, 6]
]
var isFound = false
var find = parseInt(prompt("Enter your number"))
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
if (arr[i][j] == find) {
console.log("Data is found at index number" + i + ", Row number " + j)
isFound = true;
break
}
}
}
if (!isFound) {
console.log("data is not found")
}
arr is not a two-dimensional array - you've missing a comma between the two "inner" arrays
var arr = [
[1,2,3,4,5],
// Here ---^
[0,8,7,6,6]
]
var isFound = false
var find = parseInt(prompt("Enter your number"))
for( var i =0; i <arr.length; i++) {
for( var j = 0; j < arr[i].length; j++) {
if(arr[i][j]==find) {
console.log("Data is found at index number" + i + ", Row number " + j)
isFound= true;
break
}
}
}
if (!isFound) {
console.log("data is not found")
}
Please notice that you're missing a comma between the array values. you must separate values with a comma so that javascript will know that these are two different values within the array:
var arr = [
[1, 2, 3, 4, 5],
[0, 8, 7, 6, 6]
]
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
My goal is not to use any Bubble/Section/Insertion/Merge/Quick Sort—nothing built in.
I am trying to figure out how to:
let arr = [2, 8, 7, 3]
for(let i = 0; i < arr.length; i++) {
//here I want to number use numbers as an index let's say: arr[0] is 2 right?
// I want to set number 2 at index 2 - 1
//arr[1] = at index 8 - 1
//arr[2] = at index 7 - 1
//arr[3] = at index 3 - 1
//output will be : arr [2, 3, 7, 8]
//here index of each num ^ ^ ^ ^
// 1 2 6 7
}
here I tried to remove empty items but I lost whole numbers.screenshot
let arr =[1,3,4,5,6]
let copyArr = [...arr]
copyArr[199] = 200
copyArr[149] = 150
console.log(copyArr)
let str = copyArr.toString().replaceAll(',','').split('')
console.log(str,)
// OUTPUT BELOW:
//copyArr:
// [ 1, 3, 4, 5, 6, <144 empty items>, 150, <49 empty items>, 200 ]
//str :
// [
// '1', '3', '4', '5',
// '6', '1', '5', '0',
// '2', '0', '0'
// ]
There's no need to subtract 1. Just assign each value to the corresponding index of the result array. This will work as long as all the values are non-negative integers and there are no duplicates. You don't have to ensure that the starting index is 0, because the step that removes all the empty values will take care of that.
After this, you can use a simple for loop to splice out all the undefined elements that were created in the gaps.
let result = [];
let arr = [2, 8, 7, 3];
for (let i = 0; i < arr.length; i++) {
result[arr[i]] = arr[i];
}
// remove all the empty elements
for (let i = result.length-1; i >= 0; i--) {
if (result[i] === undefined) {
result.splice(i, 1);
}
}
console.log(result);
See Looping through array and removing items, without breaking for loop for why the second loop counts down instead of up.
For your sorting question:
let arr = [2, 8, 7, 3]
// from: https://www.geeksforgeeks.org/bubble-sort-algorithms-by-using-javascript/
// Creating the bblSort function
function bblSort(arr) {
for (var i = 0; i < arr.length; i++) {
// Last i elements are already in place
for (var j = 0; j < (arr.length - i - 1); j++) {
// Checking if the item at present iteration
// is greater than the next iteration
if (arr[j] > arr[j + 1]) {
// If the condition is true then swap them
var temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
return arr;
}
console.log(bblSort(arr).map(item => item - 1))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here's my code. I wanna pull up even elements. But all I can pull up is 4 4 4 4 4.
function f6() {
let out = '';
let a6 = [[1, 2], [3, 4], [5, 6], [21, 34], [44, 56]];
for (let i = 0; i < a6.length; i++) {
for (let i = 0; i < a6[i].length; i++) {
if (a6[i][i] % 2 == 0) {
out += a6[i][i] + ' ';
}
}
}
console.log(out);
}
document.querySelector('button').onclick = f6;
<button>Push!</button>
Why?
You've used the same variable name, i, twice. Declaring let i inside your inner loop prevents you from accessing the let i from your outer loop. If you use a different variable name for iterating through each loop, e.g. j, then you should be fine.
If you only need to use the index for accessing values at that index, then you may instead want to try a for...of loop to avoid this problem altogether, e.g.:
const data = ... // 2D array
for (let row of data) {
for (let cell of row) {
// Use cell here
}
}
Your placeholder variable i is getting Shadowed by the second iteration, therefore you may want to use another placeholder such as j to have the correct reference to the element.
function f6() {
let out = '';
let a6 = [[1, 2], [3, 4], [5, 6], [21, 34], [44, 56]];
for (let i = 0; i < a6.length; i++) {
for (let j = 0; j < a6[j].length; j++) {
if (a6[i][j] % 2 == 0) {
out += a6[i][j] + ' ';
}
}
}
console.log(out);
}
document.querySelector('button').onclick = f6;
<button>Push!</button>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Suppose I have two arrays
parentArry = [1,2,3]
ChildArr = [1,2,3,4,5,6]
I want to pick to element of child array from each counter of parent array.
something like this :
1 - 1,2
2 - 3,4
3 - 5,6
Here what I am trying but it is not working
parentArry = [1,2,3]
ChildArr = [1,2,3,4,5]
for(var i=0; i<parentArry.length; i++){
console.log(ChildArr[i] + "And" + ChildArr[i+1])
}
I believe this is what you're looking for to get your expected output:
parentArry = [1,2,3]
ChildArr = [1,2,3,4,5,6]
for(var i=0; i<parentArry.length; i++){
console.log(ChildArr[i*2] + "And" + ChildArr[i*2+1])
}
What I believe you're looking for is n windows of size m where n is given by the parent array and you are generating windows from the child array.
This question is somewhat vague, but if you must approach this problem using these two arrays as such, you could do something like:
const a = [1, 2, 3];
const b = [1, 2, 3, 4, 5];
let first;
let second;
wsize = 2
for (var i = 0; i < a.length + wsize; i = i + wsize) {
first = b[i]; // could be undefined
second = b[i + 1]; // could also be undefined
if (first !== undefined && second !== undefined) {
console.log(b[i] + " and " + b[i + 1])
} else if (first !== undefined) {
console.log(b[i])
} else break
}
This is clearly an instructional answer. It is verbose and not very elegant. You'll need to be aware that the length of the first array may result in the production of undefined for the values of first and second If the length of the child array is not divisible by length of the parent array or if the parent array is longer than the child array).
There are better solutions for windowing as well which are flexible enough to handle arbitrary window sizes and arbitrary length child arrays.
What would a more elegant solution look like?
const a = [1, 2, 3];
const b = [1, 2, 3, 4, 5];
const wsize = 2;
for (var i = 0; i < a.length + wsize; i = i + wsize) {
console.log(b.slice(i, i + wsize).join(" , "))
}
Here is my guess - it is long winded but can handle the 5 items which were present when I started coding
const parentArry = [1, 2, 3]
const childArr = [1, 2, 3, 4, 5]
let cnt = 0;
parentArry.forEach(item => {
const res = [childArr[cnt]], next = childArr[++cnt];
if (next) res.push(next);
console.log(item,"-",res.join(","));
cnt++;
})
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
My code:
function diffArray(arr1, arr2) {
var newArr = [];
var arr = arr1.concat(arr2);
for(var i = 0; i < arr.length; i++)
{
for(var j = (arr.length - 1); j <= 0; j--)
{
if(i == j)
{
continue;
}
else if(arr[i] === arr[j])
{
break;
}
else
{
newArr.push(arr[i]);
}
}
}
// Same, same; but different.
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
What's wrong with my solution?
Alternative, cleaner solution using ES6 features.
const diffArray = (arr1, arr2) => {
const a = arr1.filter(v => !arr2.includes(v));
const b = arr2.filter(v => !arr1.includes(v));
return [...a, ...b];
}
console.log(diffArray([1, 2, 3, 5, 6], [1, 2, 3, 4, 5]));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have two arrays as below:
var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];
I want to compare arr2 with arr1, when it will find arr2 exactly in the same sequence as it is then it should return true. i.e. if [8,7,5] is found exactly same sequence in arr1 then it will return true.
Note:
We have to do this without using indexOf.
You could use a combination of Array#some and Array#every.
var array1 = [1, 2, 3, 8, 7, 5, 7, 2, 9, 0],
array2 = [8, 7, 5],
result = array1.some(function (a, i, aa) {
return array2.every(function (b, j) {
return aa[i + j] === b;
});
});
console.log(result);
You can loop through the largest array. On each iteration, compare the next values to all of the values found in the smaller array. If they all match, then it contains the smaller array.
var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];
console.log(doesArrayContain(arr2, arr1));
function doesArrayContain(smallestArray, biggestArray) {
for (var i = 0; i < biggestArray.length; i++) {
var doesMatch = true;
for (var j = 0; j < smallestArray.length; j++) {
if (biggestArray[i + j] !== smallestArray[j]) {
doesMatch = false; break;
}
}
if (doesMatch) {
return true;
}
}
return false;
}