Related
I am attempting to solve this kata- https://www.codewars.com/kata/stop-the-zombie-apocalypse/train/javascript
I think I have a method that should work, but is too slow/inefficient and I am getting the error "Maximum call stack size exceeded". I would greatly appreciate all responses as dumbed down as possible as I am new to this. If you could point me in the direction of a different way of doing this, or somehow tweaking my method preferably.
function findZombies(matrix) {
var n = matrix.length;
var value = 0;
//create 2 new arrays with 2 extras row and columns
var arr1 = [...Array(n + 2)].map(e => Array(n + 2).fill(value));
var arr2 = [...Array(n + 2)].map(e => Array(n + 2).fill(value));
//change arr1 so that all infected numbers = 2, everything else = 0
//leaving first and last rows and columns blank
for (var i = 0; i < n; i++) {
for (var j = 0; j < n; j++) {
if (matrix[i][j] == matrix[0][0]) {
arr1[i + 1][j + 1] = 2;
}
}
}
//if element in arr1 has a link back to arr[1][1] call the function recursively until there is no link
//Then return arr2 with changed elements.
function recur(arr1, arr2, i, j) {
if (arr1[i][j] == 2 && arr1[i][j] == arr1[i + 1][j]) {
arr2[i][j] = 1;
recur(arr1, arr2, (i + 1), j)
}
if (arr1[i][j] == 2 && arr1[i][j] == arr1[i][j + 1]) {
arr2[i][j] = 1;
recur(arr1, arr2, i, (j + 1))
}
if (arr1[i][j] == 2 && arr1[i][j] == arr1[i - 1][j]) {
arr2[i][j] = 1;
recur(arr1, arr2, (i - 1), j)
}
if (arr1[i][j] == 2 && arr1[i][j] == arr1[i][j - 1]) {
arr2[i][j] = 1;
recur(arr1, arr2, i, (j - 1))
} else {
return arr2;
console.log(arr2)
}
}
recur(arr1, arr2, 1, 1);
//clean up array by removing empty outside rows and columns
arr2.shift();
arr2.pop();
for (var i = 0; i < n; i++) {
arr2[i].shift();
arr2[i].pop()
}
console.log(arr2);
}
var matrix = [
[9, 1, 2],
[9, 9, 9],
[7, 4, 9],
[7, 9, 7]
];
var matrix2 = [
[8, 2, 3, 8, 8],
[8, 0, 8, 8, 8],
[1, 2, 8, 4, 8],
[8, 2, 3, 8, 8],
[8, 8, 8, 0, 5]
];
findZombies(matrix)
You could store all valid points (zombies) in a nested hash table with the all zombies and their adjacent zombies.
At the end start with the known zombie at [0][0] and take the array of the ahsh table to visit all connected field. For preventing visiting already visited items, replace the array with undefined.
function findZombies(matrix) {
function go([i, j]) {
if (!tree[i] || !tree[i][j]) return;
result[i][j] = 1;
var temp = tree[i][j];
tree[i][j] = undefined;
temp.forEach(go);
}
var i, j,
result = [],
zombie = matrix[0][0],
tree = {};
for (i = 0; i < matrix.length; i++) {
result.push([]);
for (j = 0; j < matrix[i].length; j++) {
result[i].push(0);
if (matrix[i][j] !== zombie) continue;
if (!tree[i]) tree[i] = {};
tree[i][j] = [[i, j - 1], [i, j + 1], [i - 1, j], [i + 1, j]].filter(([x, y]) => matrix[x] && matrix[x][y] === zombie);
}
}
go([0, 0]);
return result;
}
var matrix = [[9, 1, 2, 3, 4, 1, 2, 9], [9, 9, 9, 2, 1, 5, 9, 9], [9, 2, 9, 3, 7, 9, 1, 9], [6, 9, 9, 9, 0, 9, 2, 9], [5, 4, 3, 9, 9, 9, 4, 9], [9, 3, 9, 5, 8, 9, 9, 9], [9, 9, 9, 9, 9, 9, 7, 9], [9, 9, 1, 2, 3, 9, 8, 9]],
result = findZombies(matrix);
result.forEach(a => console.log(...a));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I need to find the index of the first duplicated number in an array and assign it to an empty variable using only for loop
Thanks in advance
i have tried many logical operators.
var findIndex;
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 0
var arrWithNumbers = [3, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 1
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
if (arrWithNumbers[i] === i) {
firstIndex = arrWithNumbers.indexOf(i);
break;
}
}
console.log(firstIndex);
what I expect:
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 0
var arrWithNumbers = [3, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 1
//what i have
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
if (arrWithNumbers[i] === i) {
firstIndex = arrWithNumbers.indexOf(i);
break;
}
}
console.log(firstIndex); // 2
One option you have is to have a variable that contains all the count of the number, you can do this by using reduce
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
var numberCount = arrWithNumbers.reduce((c, v) => (c[v] = (c[v] || 0) + 1, c), {});
for (var i = 0; i < arrWithNumbers.length; i++) {
if (numberCount[arrWithNumbers[i]] > 1) {
firstIndex = i;
break;
}
}
console.log(firstIndex);
Another option is using lastIndexOf. If the current index is not the same as the lastIndexOf value, means that it has duplicate and break the loop.
var arrWithNumbers = [3, 2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
if (i !== arrWithNumbers.lastIndexOf(arrWithNumbers[i])) {
firstIndex = i;
break;
}
}
console.log(firstIndex);
You could take a hash table for visited values and store their indices. Then you need only the check if the hash property is set and return the index.
This approach works with a single loop and exits early on the first found same value.
function findIndex(array) {
var indices = Object.create(null),
i, value;
for (i = 0; i < array.length; i++) {
value = array[i];
if (value in indices) return indices[value];
indices[value] = i;
}
}
console.log(findIndex([2, 4, 5, 2, 6, 5, 1, 2, 4, 8])); // 0
console.log(findIndex([3, 4, 5, 2, 6, 5, 1, 2, 4, 8])); // 2
You can use a nested for loop, to check all values after index i in your array :
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
value_i = arrWithNumbers[i]
// loop through the next items of the array
for (var j = i+1 ; j < arrWithNumbers.length; j++) {
if (value_i == arrWithNumbers[j]) {
firstIndex = i;
break;
}
}
if (firstIndex !== null) {
// we found our firstIndex, quit the main loop
break;
}
}
console.log(firstIndex)
I am taking an excercise on codewars:
Given a list of integers and a single sum value, return the first two
values (parse from the left please) in order of appearance that add up
to form the sum.
Example:
sum_pairs([10, 5, 2, 3, 7, 5], 10)
# ^-----------^ 5 + 5 = 10, indices: 1, 5
# ^--^ 3 + 7 = 10, indices: 3, 4 *
# * entire pair is earlier, and therefore is the correct answer
== [3, 7]
What do you think entire pair is earlier means? IMO if the sum of it's indexes is smallest. Now based on this assumption I made my solution and one test fails:
var sum_pairs=function(ints, s){
let i = 0;
let pair = [0, 0];
let ind = [100, 100]
let found = false;
function loop(i) {
if (i > ints.length) return pair;
ints.slice(i).forEach((curr, idx) => {
ints.slice(i+1).some((num, i) => {
let sum = curr + num;
let prevIndicies = ind[0] + ind[1];
if(sum === s && prevIndicies > idx + i) {
ind = [idx, i];
pair = [curr, num];
found = true;
return true;
}
})
})
i += 1;
loop(i)
}
loop(0)
if (found) {
return pair
}
return undefined;
}
console.log(sum_pairs([1,4,8,7,3,15], 8))
Test returns error that [1, 7] is expected.
I'm pretty sure what it means is they want the second element to be as leftward in the list as possible. For example, for
l5= [10, 5, 2, 3, 7, 5];
when trying to find a sum of 10, the desired output is
[3, 7]
[10, 5, 2, 3, 7, 5];
^ ^
instead of
[5, 5]
[10, 5, 2, 3, 7, 5];
^ ^
because the last element in [3, 7], the 7, came before the second 5.
This code seems to pass all test cases - iterate in a triangular fashion, starting at indicies [0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3], ...:
const sum_pairs = function(ints, s){
const { length } = ints;
for (let i = 1; i < length; i++) {
for (let j = 0; j < i; j++) {
if (ints[i] + ints[j] === s) return [ints[j], ints[i]];
}
}
}
const sum_pairs=function(ints, s){
const { length } = ints;
for (let i = 1; i < length; i++) {
for (let j = 0; j < i; j++) {
if (ints[i] + ints[j] === s) return [ints[j], ints[i]];
}
}
}
l1= [1, 4, 8, 7, 3, 15];
l2= [1, -2, 3, 0, -6, 1];
l3= [20, -13, 40];
l4= [1, 2, 3, 4, 1, 0];
l5= [10, 5, 2, 3, 7, 5];
l6= [4, -2, 3, 3, 4];
l7= [0, 2, 0];
l8= [5, 9, 13, -3];
console.log(sum_pairs(l1, 8))
console.log(sum_pairs(l2, -6))
console.log(sum_pairs(l3, -7))
console.log(sum_pairs(l4, 2))
console.log(sum_pairs(l5, 10))
console.log(sum_pairs(l6, 8))
console.log(sum_pairs(l7, 0))
console.log(sum_pairs(l8, 10))
It means that you go from left to right and take the first matching pair, and since 7 is the first element that creats a pair (going from the left) 3 and 7 is the first pair.
I would solve it a bit easier:
function sum_pairs(arr, target) {
let old = [];
let result = [];
arr.some((el) => {
let found = old.find((oldEl) => oldEl + el === target);
if (found) return result = [found, el];
old.push(el);
})
return result;
}
sum_pairs([10, 5, 2, 3, 7, 5], 10);
Edit: an explaination. I loop over all elements in the array searching for a match i all the elements I have passed. If I find a match I remember it and break out of the loop by returning a "truthy" value. (That is just how .some() works.) Finally if I have not found a match I add the element to my list of old elements and go on to the next.
function sum_pair(arr,sum){
let result = [];
arr.forEach((i, j)=>{
if(i+arr[j+1]===sum){
console.log(i,arr[j+1], i+arr[j+1])
}
})
}
sum_pair([0, 3, 7, 0, 5, 5],10)
What is an efficient way of looping through two arrays to produce an alternating output? In JavaScript.
If I have two arrays like this:
var oddNumbers = [1, 3, 5, 7, 9]
var evenNumbers = [2, 4, 6, 8, 10, 12, 14]
NB: The arrays may not be the same length
How can I get the following output?
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14
I would have thought this would work:
if (oddNumber.length > evenNumbers.length) {
var large = oddNumbers;
} else {
var large = evenNumbers;
}
for(var i = 0; i < large.length; i++){
if (evenNumbers.length >= i && oddNumbers.length >= i) {
console.log(oddNumbers[i] + ", " + evenNumbers[0]);
} elseif (evenNumbers.length >= i) {
console.log(evenNumbers[0]);
} else {
console.log(oddNumbers[0]);
}
}
But it's pretty messy, any better way of approaching this?
NOTE: These may not necessarily be in a numerical order, or in fact numbers
I would rather do it as follows if you just want to output them:
var oddNumbers = [1, 3, 5, 7, 9];
var evenNumbers = [2, 4, 6, 8, 10, 12, 14];
for (var i=0, j=0; i < oddNumbers.length || j < evenNumbers.length;) {
if (i < oddNumbers.length) {
console.log(oddNumbers[i++]);
}
if (j < evenNumbers.length) {
console.log(evenNumbers[j++]);
}
}
If you want to get the merge result as another array you can replace console.log with result.push to push result values on an array named result as follows:
var oddNumbers = [1, 3, 5, 7, 9];
var evenNumbers = [2, 4, 6, 8, 10, 12, 14];
var result = [];
for (var i=0, j=0; i < oddNumbers.length || j < evenNumbers.length;) {
if (i < oddNumbers.length) {
result.push(oddNumbers[i++]);
}
if (j < evenNumbers.length) {
result.push(evenNumbers[j++]);
}
}
console.log(result);
This way you iterate both arrays as long as one of them has an element that we haven't visited yet and also prevents iterating over the same index of same array twice. Please note that I used increment in if blocks to save 2 lines of code. You can also move them to the for loop since they won't break if statements.
var oddNumbers = [1, 3, 5, 7, 9];
var evenNumbers = [2, 4, 6, 8, 10, 12, 14];
var oLength = oddNumbers.length;
var eLength = evenNumbers.length;
var n = oLength > eLength ? oLength : eLength;
var rez=[];
for(i=0;i<n;i++){
if (i< oLength) rez.push(oddNumbers[i])
if (i<eLength) rez.push(evenNumbers[i])
}
console.log(rez);
var odd = ["A", "C","E","G"];
var even = ["B","D","F"];
var rez=[];
for(i=0;i<(odd.length > even.length ? odd.length : even.length);i++){
if (i< odd.length) rez.push(odd[i])
if (i<even.length) rez.push(even[i])
}
console.log(rez);
The following function accepts two arrays and returns their interleaved values as a new array:
function interleaveArrays(a, b) {
var array = [],
limit = a.length >= b.length ? a.length : b.length;
index = 0;
while (index < limit) {
a[index] && array.push(a[index]);
b[index] && array.push(b[index]);
index += 1;
}
return array;
}
Calling the function like so:
var oddNumbers = [1, 3, 5, 7, 9],
evenNumbers = [2, 4, 6, 8, 10, 12, 14];
console.log(interleaveArrays(oddNumbers, evenNumbers));
Yields:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14 ]
You can then output this in your preferred manner; e.g:
var interleaved = interleaveArrays(oddNumbers, evenNumbers);
// as a loop
interleaved.forEach(function (n) {
console.log(n);
})
// or as a string
console.log(interleaved.join(', '));
// etc.
Hope this helps :)
I'd do something like this.
large = (oddNumber.length >= evenNumbers.length) ? oddNumbers : evenNumbers;
small = (oddNumber.length < evenNumbers.length) ? oddNumbers : evenNumbers;
for(var i = 0; i < large.length; i++){
if(small.length <= i + 1){
console.log(small[i] + ", "+ large[i]);
}
else {
console.log(large[i]);
}
}
A long-hand example of how it can be done. The code can be shrunk for a final solution. The basic principle I'm using is to even out the lengths to take care of the alternating then tag on the tail
var oddNumbers = [1, 3, 5, 7, 9];
var evenNumbers = [2, 4, 6, 8, 10, 12, 14];
var oLength = oddNumbers.length;
var eLength = evenNumbers.length;
var oTemp, eTemp, remainder;
if(oLength > eLength) {
eTemp = evenNumbers;
oTemp = oddNumbers.slice(0, eLength);
remainder = oddNumbers.slice(eLength);
} else if (eLength > oLength) {
eTemp = evenNumbers.slice(0, oLength);
oTemp = oddNumbers;
remainder = evenNumbers.slice(oLength);
} else {
eTemp = evenNumbers;
oTemp = oddNumbers;
remainder = [];
}
var final = [];
for(var i=0; i < eTemp.length; i++) {
final.push(oTemp[i]);
final.push(eTemp[i]);
}
final = final.concat(remainder);
alert(final);
I would simply merge the two array and sort it
var oddNumbers = [1, 3, 5, 7, 9];
var evenNumbers = [2, 4, 6, 8, 10, 12, 14];
var mergedArr=oddNumbers.concat(evenNumbers );
console.log(mergedArr.sort(function(a,b){return a-b;}));
See No loop.. No hassle. Very Simple
There will be an extra , on the screen. Add an if statement if you don't want that
for(var i = 0; i < large.length; i++){
if(i<evenNumbers.length)
console.log(evenNumbers[i]+",");
if(i<oddNumber.length)
console.log(evenNumbers[i]+",");
}
try this it will work always either number Array or String Array:
var oddNumber = [1, 3, 5, 7, 9]
var evenNumber = [2, 4, 6, 8, 10, 12, 14]
var margedNumbers = oddNumber.concat(evenNumber);
console.log("before: "+margedNumbers);
margedNumbers.sort(function(a, b){return a-b})
console.log("after: "+margedNumbers)
My solution
var oddNumbers = [1, 3, 5, 7, 9]
var evenNumbers = [2, 4, 6, 8, 10, 12, 14]
var extraElements = (oddNumbers.length > evenNumbers.length) ? oddNumbers.slice(evenNumbers.length) : evenNumbers.slice(oddNumbers.length);
var finalArr = [];
var small = (oddNumbers.length < evenNumbers.length) ? oddNumbers : evenNumbers;
small.forEach((each, index) => {
// merge elements in desired order
finalArr.push(oddNumbers[index]);
finalArr.push(evenNumbers[index]);
})
finalArr = finalArr.concat(extraElements);
alert(finalArr);
Extract the extra elements which makes both array of same length. Then, in a simple iteration, push elements from both array with same index.
I'm trying to create a sum function. When I run it through two different arrays (with same values), it's giving me different results. I can't really tell where I did wrong. It seems when I'm generating the array using the 'range' function, it's looping twice.
var myArr = [];
var tempArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function range(start, end) {
for (i = start; i <= end; i++) {
myArr.push(start);
start = start + 1;
}
return myArr;
}
function sum(arr) {
var sumArr = 0;
for (i = 0; i < arr.length; i++) {
sumArr = sumArr + arr[i];
//console.log(sumArr);
}
return sumArr;
}
console.log(range(1, 10)); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
console.log(tempArr); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
console.log(sum(range(1, 10))); //110
console.log(sum(tempArr)); //55
Any help would be appreciated. Thanks!
The reason is that var myArr = []; was a global variable. So pushed elements in the first console attempt will be there until they are cleared. You can use local variable in the function instead.
var tempArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function range(start, end) {
var myArr = [];
for (i = start; i <= end; i++) {
myArr.push(start);
start = start + 1;
}
return myArr;
}
function sum(arr) {
var sumArr = 0;
for (i = 0; i < arr.length; i++) {
sumArr = sumArr + arr[i];
//console.log(sumArr);
}
return sumArr;
}
console.log(range(1, 10)); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
console.log(tempArr); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
console.log(sum(range(1, 10))); //55
console.log(sum(tempArr)); //55
Using lodash :
You can use ._sum function.
var tempArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = _.sum(tempArr)
Don't forget to add the library if you want to use it.
<script src="https://cdn.jsdelivr.net/lodash/4.5.1/lodash.min.js"></script>
Demo