Adding a Unique Element to an Array - javascript

I have an array of objects called seasons of length 300, and I trying to search through a certain property "Date" and add it to an array if it has not been found before. So far I have
var day=[];
for (var i=1; i<300; i++) {
var found=false;
for (var j=0; j<day.length; j++) {
if (day[j]==seasons[i]["Date"]) {
found=true;
break;
}
if (!found) {
day[day.length]=seasons[i]["Date"];
}
}
}
I'm not too sure where this is going wrong, and would appreciate some help. Thanks

You break out of the inner for-loop, so the if (!found) block is never executed.
Just put it after the inner loop:
for (var i = 1; i < 300; i++) {
var found = false;
for (var j = 0; j < day.length; j++) {
if (day[j] == seasons[i]["Date"]) {
found = true;
break;
}
}
if (!found) {
day[day.length] = seasons[i]["Date"];
}
}
Or do it in the if-block:
for (var i = 1; i < 300; i++) {
for (var j = 0; j < day.length; j++) {
if (day[j] == seasons[i]["Date"]) {
day[day.length] = seasons[i]["Date"];
break;
}
}
}
I guess the latter solution is easier to understand.

Related

Processing error "Syntax error expected ; but found i"

I am trying to write a matrix library in processing using javascript but keep getting the error above on line 5. I can't seem to spot what is causing the error so any help would be appreciated.
The goal is to implement the matrix product function.
function Matrix(rows,cols){
this.rows = rows;
this.cols = cols;
this.data = [];
for(var i = 0; i < this.rows; i++){
//assign every row an array
this.data[i] = [];
for (var j = 0; j < this.cols; j++){
//assign every column an array
this.data[i][j] = 0;
}
}
}
Matrix.prototype.multiply = function(n){
if(n instanceof Matrix){
// Matrix product
if (this.cols !== n.rows) {
console.log('Columns of A must match rows of B.');
return undefined;
}
let result = new Matrix(this.rows, n.cols);
for (let i = 0; i < result.rows; i++) {
for (let j = 0; j < result.cols; j++) {
// Dot product of values in col
let sum = 0;
for (let k = 0; k < this.cols; k++) {
sum += this.data[i][k] * n.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
else{
for(var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++){
//multiply scalar
this.data[i][j] *= n;
}
}
}
}
I found the error it was just a curly bracket in the wrong place!

Create and initialize bidimensional array in javascript

I am trying to create and initialize a bidimensional array in javascript inside an AngularJS application as follows:
$scope.invalidVote = [];
for (var i = 0; i < $scope.arry1.length; i += 1) {
$scope.answersCount[i] = $scope.arry1[i].arry2.length;
for(var j = 0; j < $scope.arry1[i].arry2.length; j += 1) {
$scope.invalidVote[i][j] = false;
}
}
But it doesn't work, What is the right way to do that?
try this:
$scope.invalidVote = [];
for (var i = 0; i < $scope.arry1.length; i++) {
$scope.answersCount[i] = $scope.arry1[i].arry2.length;
$scope.invalidVote[i] = [];
for(var j = 0; j < $scope.arry1[i].arry2.length; j++) {
$scope.invalidVote[i][j] = false;
}
}
I'm assuming $scope.arry1[i] is an array that contain other arrays and is already fill with values.
So your code should look like:
$scope.invalidVote = $scope.arry1;
for (var i = 0; i < $scope.arry1.length; i += 1)
{
$scope.answersCount[i] = $scope.arry1[i].length;
for(var j = 0; j < $scope.arry1[i].length; j += 1)
{
$scope.invalidVote[i][j] = false;
}
}
'$scope.invalidVote = $scope.arry1;' declaring "invalidVote" like this ensure it contains the same amount of indexes.

unexpected result in function separating unique and non unique items

The aim of the bellow function is to output only the non-unique items from an array that is passed as an argument:
"use strict";
function nonUnique(data){
var tab = [];
for(var d = 0; d < data.length; d++) {
if(typeof(data[d]) == "string"){
tab[d] = data[d].toUpperCase();
}
else{
tab[d] = data[d];
}
}
var count = 0;
var tab_non_unique = [];
var tab_unique = [];
for(var i = 0; i < tab.length; i++){
for(var j = 0; j < tab.length; j++){
if(tab[i] == tab[j]){
count ++;
}
if(count > 1){
tab_non_unique.push(tab[i]);
count = 0;
break;
}
if (count == 1) {
tab_unique.push(tab[i]);
}
}
}
return tab_non_unique;
}
I have tested the function by calling it on different arrays but somehow on
nonUnique([1, 2, 3, 4, 5]);
it fails by returning:
=> [ 2, 4 ]
I don't understand what in my code causes 2 and 4 to raise the counter higher than 1 and thus end up in the tab_non_unique array. Any help would be greatly appreciated, thanks.
The problem is that you reset count only if a non-unique is found. But it should be reset always when starting with a new number.
So put the count=0 at the top of the loop.
for (var i = 0; i < tab.length; i++) {
count = 0;
for (var j = 0; j < tab.length; j++) {
if (tab[i] == tab[j]) {
count++;
}
if (count > 1) {
tab_non_unique.push(tab[i]);
break;
}
if (count == 1) {
tab_unique.push(tab[i]);
}
}
}

how not to get out of array boundaries

I have an array and by looping I compare cell with a cell near it. I get the 'out of range' exception,
How can I fix it ?
for (var i = 0; i < array.length ; i++) {
if ((++array[i] == array[i+1])) {
alert("yes");
}
else {
alert("no");
}
}
Run your loop from for (var i=0; i<array.length -1; i++) instead (because you compare against array[i+1])
Just try with:
for (var i = 0; i < array.length - 1; i++) {}
Not sure what you had in mind but using ++ outside of a for loop is never a good idea as it can be confusing. Use another variable to point to another item in the array while looping together with a bounds checker is easier to debug and keeps loop simple.
//displays 01010
//1=2(0),2=2(1),2=4(0),4=4(1),4=5(0)
var ptr = 0;
var items = new Array (1, 2, 2, 4, 4, 5);
for (var i = 0; i < items.length; i++) {
ptr++
if(ptr >= items.length)break;
if (items[i] == items[ptr]) {
console.log(1);
}
else {
console.log(0);
}
}
//or
//displays 10001 as each value is increased then compared
//2=2(1),3=2(0),3=4(0),5=4(0),5=5(1)
ptr = 0;
for (var i = 0; i < items.length; i++) {
ptr++
if (ptr >= items.length) break;
if (++items[i] == items[ptr]) {
console.log(1);
}
else {
console.log(0);
}
}

Compare two arrays in javascript

I'd like to check which elements are equal in my two arrays, but can't get it working.
This is my code:
for (var i; i < bombs.length; i++) {
for (var j; j < bombsDb.length; j++) {
if (bombs[i].name === bombsDb[j].address) {
console.log(bombs[i].name);
} else {
console.log("non-equal elements");
}
}
}
So the first array contains objects from the google places api and the second one contains data from my database.
Thanks in advance!
You have to initialize i and j;
for (var i = 0; i < bombs.length; i++) {
for (var j = 0; j < bombsDb.length; j++) {
if (bombs[i].name === bombsDb[j].address) {
console.log(bombs[i].name);
} else {
console.log("non-equal elements");
}
}
}
Comparing can also be done using the .not selector from jquery. Check this:
var a = [1,2,3,4,5,6];
var b = [4,5,6,7,8,9];
$(a).not( $(a).not(b).get() ).get();
This will return the following array
[4,5,6]
You are missing the initial assignment to i and j in your for loop.
// here
// v
for (var i = 0; i < bombs.length; i++) {
// your loop
}
This causes the comparision to return false in the first iteration of the loop since undefined < bombs.length always return false, so it will not proceed.

Categories