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
This function should swap the values in the array A that are at position i and j. For example, if A = [3,2,1,6,4]
swap(A, 2,3) should change A so it is now:
A = [3,2,6,1,4]
we're swapping the 1 at position 2 and the 6 at position 3 so now the 6 is at position 2 and the 1 is at position 3.
Problem is, I do not know how to swap these! (New to programming)
function swap(A, i, j) {
var swap = myArray[j];
myArray[j] = myArray[i];
myArray[i] = swap;
}
const A = [3, 2, 1, 6, 4];
console.log(swap(A, 2, 3))
Just need to pass the array and swapping indexes. You need to get the value at index i and j using array index as myArray[i] or myArray[j]
function swap(myArray, i, j) {
var swap = myArray[j];
myArray[j] = myArray[i];
myArray[i] = swap;
}
const array = [1, 2, 3, 4, 5, 6];
swap(array, 1, 3);
console.log(array)
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 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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
x = [1, 2, 3, 4]
t = []
c = []
for(i = 0; i<x.length; i++){
function solution(){
t = Math.pow(x, 2)
c = t[i]++
return c
}
}
The function needs to:
Count the squares of the array numbers
Then to get sum of squared (I am not sure if I wrote it correct, LOL) numbers. Thanks!
const result = [1, 2, 3, 4].map(n => n ** 2).reduce((acc, val) => acc + val, 0);
console.log(result);
You can also use ** operator which is faster than Math.pow(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
var result = [1, 2, 3, 4].map(n => Math.pow(n, 2)).reduce((acc, val) => acc + val, 0);
console.log(result);
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 details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
var array = [0, 1, 2, 3];
var numbers = [3, 6 ,2, 7];
I want to multiply each number in array with numbers without repetition of numbers from numbers
Fixed number for all index
var array = [0, 1, 2, 3];
array.map(
function(n){
return (n* number to be multiplied);
}
);
Different number for each index
var array = [0, 1, 2, 3], numberToBeMultiplied = [1,3,5,7];
array.map(
function(n, i){
return n * numberToBeMultiplied[i];
});
You can also push the returning elements in an array.
Your question is not clear enough, what I get you may want to do something like this.
var array = [1,2,3,4]
var array2 = [];
for(var i = 0; i < array.length; i++) {
var randomMultiplier = Math.floor((Math.random() * 10) + 1); //Return a random number between 1 and 10
array2.push(array[i] * randomMultiplier);
$('#multiplied-list').append('<li>'+array[i] * randomMultiplier+'</li>')
}
console.log(array2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="multiplied-list"></ul>