Javascript - Creates new array instead of incrementing - javascript

I'm making a simple twitter app to work on my javascript.
The code below is supposed to identify every tweets location and count the number of tweets per location.
However, it doesn't increment, it just creates a new array.
What is wrong with my code? How can I make it better?
Thank you
var Twitter = require('node-twitter'),
twit = {},
loc = [];
twit.count = 0;
var twitterStreamClient = new Twitter.StreamClient(
//credentials
);
twitterStreamClient.on('close', function () {
console.log('Connection closed.');
});
twitterStreamClient.on('end', function () {
console.log('End of Line.');
});
twitterStreamClient.on('error', function (error) {
console.log('Error: ' + (error.code ? error.code + ' ' + error.message : error.message));
});
twitterStreamClient.on('tweet', function (tweet) {
if (loc.indexOf(tweet.user.location) === -1) {
loc.push({"location": tweet.user.location, "locCount": 1});
} else {
loc.loation.locCount = loc.loation.locCount + 1;
}
console.log(loc);
});
var search = twitterStreamClient.start(['snow']);

You need to rewrite on tweet callback:
var index = loc.reduce(function(acc, current, curIndex) {
return current.location == tweet.user.location ? curIndex : acc;
}, -1);
if (index === -1) {
loc.push({"location": tweet.user.location, "locCount": 1});
} else {
loc[index].locCount++;
}

Array.indexOf is not matching as you think it is. You're creating a new object and pushing it into the array, and regardless of whether its properties match a different object perfectly, it will not be === equal. Instead, you have to find it manually:
var foundLoc;
for (var i = 0; i < loc.length; i++) {
if (loc[i].location.x === location.x)
foundLoc = loc[i];
break;
}
}
if (!foundLoc) {
loc.push({location: location, count: 0});
} else {
foundLoc.count++
}

Related

Matching strings don't trigger condition in Javascript

I am trying to do a very simple stock counting app, and display dynamically the stocks for each item. When I try to pair the button with its item, it doesn't work, even though the 2 strings are actually a match.
Here is the faulty function:
socket.on('Stock init', (data) => {
items = data.items;
displayStock(items);
});
function displayStock(items) {
fridgeItem.forEach((fridgeItem) => {
for (let i = 0; i < items.length; i++) {
var databaseProduct = items[i].name;
var databaseQuantity = items[i].chilled.toString();
console.log(fridgeItem.textContent, databaseProduct);
}
if (fridgeItem.textContent === databaseProduct) {
fridgeItem.textContent = databaseProduct + ' ' + databaseQuantity;
} else {
console.log('err');
}
});
}
Answer might be stupid but I can't see it at the moment. Thanks

then not working as expected in JS promise

I'm trying to extract text from a pdf and then return a number that represents how many pages of the pdf are matched by a regex that I define.
My problem is that, rather than periodically checking whether or not the text of a single page is part of the match, my function divides the pieces up into smaller sections than pages. Count is meant to increment only after an entire page has been read.
getnopages: function(){
var fulltext = ""
var partialmatch;
var somerx = /something/
return pdfjs.getDocument(data).then(function(pdf) {
var pages = [];
pageNumbers = [];
for (var i = 0; i <= 6; i++) {
pages.push(i);
}
var found = false;
var count = 1;
return Promise.all(pages.map(function(pageNumber) {
pageNumbers.push(pageNumber);
return pdf.getPage(pageNumber + 1).then(function(page)
return page.getTextContent().then(function(textContent) {
return textContent.items.map(function(item) {
fulltext+=item.str+'&&&';
return item.str;
}).join('&&&');
});
}).then(function(){
count++;
console.log('the count is ' + count)
var partialmatch;
try {
partialmatch = fulltext.match(somerx)[0]
console.log('the match: ' + partialmatch)
var full = fulltext.slice(0, fulltext.length-3)
console.log('the full text ' + full)
if (fulltext && partialmatch!==full && !found){
found = true;
console.log('now we found our number: ' + count) // this finds where the full text differs from the partial text but returns a number too large to be a page number
}
}
catch(e){
console.log(e)
}
});
}));
}
Can anyone help me figure out how to rewrite this so that count is incrementing page numbers correctly?
I don't really know where is the problem in your code but I just suggest you to avoid too many nestings with promises. You can reduce nesting by chaining your promise like below:
getnopages: function() {
var somerx = /something/
return pdfjs.getDocument(data).then(function(pdf) {
var pages = [];
pageNumbers = [];
for (var i = 0; i <= 6; i++) {
pages.push(i);
}
var found = false;
var count = 1;
var promises = pages.map(pageNumber => {
pageNumbers.push(pageNumber);
return pdf.getPage(pageNumber + 1).then(page => {
return page.getTextContent();
}).then(textContent => {
return textContent.items.map(item => {
fulltext += item.str +'&&&';
return item.str;
}).join('&&&');
});
});
return Promise.all(promises).then(() => {
...
});
});
}

Displaying all the items in an array using jquery and json

I am trying to get all the items in an array to show using json and jquery from the song of ice and fire api. I can only get one item to show from each of the arrays.
Here is the codepen: https://codepen.io/frederickalcantara/pen/aWeXOz
var data;
$.getJSON(characters[i], function(json) {
data = json;
var alliance = $('#alliance');
for (var i = 0; i < data.allegiances.length; i++) {
if (i === data.allegiances.length - 1) {
$.getJSON(data.allegiances[i], function(json1) {
alliance.html(json1.name);
});
} else {
$.getJSON(data.allegiances[i], function(json1) {
alliance.html(json1.name + ', ');
});
}
}
const title = $('#title');
if (data.titles.length === "") {
return 'N/A';
} else {
for (i = 0; i < data.titles.length; i++) {
if (i === data.titles.length - 1) {
title.html(data.titles[i]);
} else {
title.html(data.titles[i]) + ', ';
}
}
const tv = $('#seasons');
for (var i = 0; i < data.tvSeries.length; i++) {
if (i === data.tvSeries.length - 1) {
tv.html(data.tvSeries[i]);
} else {
tv.html(data.tvSeries[i] + ', ');
}
}
const actor = $('#actors')
if (json.playedBy === "") {
return 'N/A';
} else {
actor.html(json.playedBy);
}
});
The main problem is your loop. You keep replacing the value in the html element until the last value in the array. You can simplify this code like this:
title.html(data.titles.join(','));
which replaces all of this:
for (i = 0; i < data.titles.length; i++) {
if (i === data.titles.length - 1) {
title.html(data.titles[i]);
} else {
title.html(data.titles[i]) + ', ';
}
}
Update: Handling the allegiances.
Using a Promise here is important because you are making a number of AJAX calls and you need to be sure that they are resolved before attempting to display them. You can replace the entire for loop for the allegiances like this:
Promise.all(data.allegiances.map(function(ally){
return new Promise(function(resolve, reject){
$.getJSON(ally, function(json) {
resolve(json.name);
});
});
}))
.then(function(allies){
alliance.html(allies.join(', '));
});

Protractor- Retrieve value of global array in a function

I am trying to push array content in a global defined empty array & then retrieve its contents inside another function.
Below is the code which i tried:
describe('My Test', function() {
var arrayf3=[];
var indexf3='not found';
it('Test starts', function() {
browser.ignoreSynchronization = true;
browser.get('https://www.w3schools.com/angular/');
var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]'));
elm.count().then(function(count) {
Methods.pushToArray(0, count, elm);
})
var texttocheck='Data Binding';
Methods.getIndex(0, arrayf3.length, arrayf3, texttocheck);
console.log('Text content of global array is ' + arrayf3);
console.log('index of the array number having texttofind is ' + indexf3);
})
var Methods = {
getIndex :function (i, max, array, texttocheck) {
if (i < max) {
console.log('text[' + i + '].indexOf = ' + array[i].indexOf(texttocheck))
if (array[i].indexOf(texttocheck) > 0) {
indexf3 = i;
} else {
Methods.getIndex(i + 1, max, array, texttocheck);
}
}
},
pushToArray :function (i, max, elm) {
if (i < max) {
elm.get(i).getText().then(function(tmpText) {
console.log("The array "+tmpText);
arrayf3.push(tmpText);
})
Methods.pushToArray(i + 1, max, elm);
}
},
}
});
Problem is i am getting null values for below placeholder values:
Text content of global array is
index of the array number having texttofind is
I want the array value copied in this global empty array to be used & displayed in the same it block function 'Test starts'
Protractor's element.all inherently knows how to getText() on each of the elements and return the values as an array.
it('Test starts', function() {
browser.ignoreSynchronization = true;
browser.get('https://www.w3schools.com/angular/');
var getIndexOfElementByPartialText = function(inputText) {
return element(by.id('leftmenuinner')).all(by.css('[target="_top"]')).getText().then(function(values) {
var indexNumber;
values.forEach(function(value, index) {
if (new RegExp(inputText).test(value)) {
if (indexNumber === undefined) {
indexNumber = index;
} else {
throw new Error('multiple elements match the input text');
}
}
});
if (indexNumber === undefined) {
throw new Error('no elements match the input text');
} else {
return indexNumber;
}
});
});
expect(getIndexOfElementByPartialText('thing1').toBe(1);
expect(getIndexOfElementByPartialText('thing2').toBe(2);
});
Edited the answer to provide it in terms of a re-usable function.

Need help comparing the value inside array, with forEach loop

So basically I have some promise, forEach, just a lot of issues with this single problem I need to solve. So the variables I work with has below structure:
persons = [object, object, object]
where each object has { user:number , username: string, latitude:number, longitude:number}
from there I try to figure out if my user/username is inside of one of these objects, if not found id like it to be created, if found found id like it to update their location. Sounds simple, I think the problem has blown out of proportion but nothing works. The code I have now does not work, its either I can never figure out when the user is not there, or I can not figure out how to get to stop creating me every time it find a user who is not me.
var getswamp = function(item, index) {
return new Promise(function(resolve, reject){
var result = false;
if (item.user === user && item.username === username) {
if ((item.latitude !== latitudenew) || (item.longitude !== longitudenew)) {
var id = item.id;
swampdragon.update('locationcurrent', {
user: user,
latitude: latitudenew,
longititude: longitudenew,
username: username,
id: id
}, function (context, data) {
console.log("data updated", data);
result = true;
resolve(result);
}, function (context, data) {
console.log("You may not be updated");
});
} else {
console.log("No location change");
result = true;
}
}else{
if ( item.index === person.index){
console.log(person);
resolve(result)
}
}
});
};
person.forEach(function (item, index) {
var swamping = getswamp(item, index);
swamping.then(function (result) {
console.log(result);
if (result === true) {
console.log("We have you");
} else if (result === false && (index === person.length - 1)) {
console.log('Index: ' + index + ' Length of list is ' + person.length);
swampdragon.create('locationcurrent', {
user: user,
latitude: latitudenew,
longititude: longitudenew,
username: username
}, function (context, data) {
console.log("data created", data);
}, function (context, data) {
console.log("You may not be created")
});
}
})
});
Any help/ideas would just be great.
The Promise is used when some asynchronous event happened.
Since I can not create such event, I made a static code as below:
var persons = new Array();
persons.indexOf = function(person) {
var index = -1;
this.forEach(function(obj, i) {
if (person.username == obj.username) {
index = i;
}
});
return index;
}
persons.addOrUpdate = function(person) {
var index = this.indexOf(person);
if (index == -1) {
person.user = this.length + 1;
this.push(person);
}
else { // update case
var obj = this[index];
obj.latitude = person.latitude;
obj.longitude = person.longitude;
}
}
persons.print = function() {
var str = "";
this.forEach(function(obj) {
str += obj.user + ". " + obj.username + " at location (" +
obj.latitude + ":" + obj.longitude + ")\n";
});
return str;
}
persons.addOrUpdate({username:'Adam', latitude:-0.0045, longitude:14.2015});
persons.addOrUpdate({username:'Eve', latitude:-0.0045, longitude:14.2015});
persons.addOrUpdate({username:'Abel', latitude:-0.0045, longitude:14.2015});
// Updating Able location
var person = {username:'Abel', latitude:10.1145, longitude:14.1234};
persons.addOrUpdate(person);
alert(persons.print());

Categories

Resources