loop and condition in ajax not correct - javascript

responseText like "insert-90RS252,insert-90RS262,insert-90RS232"
function(responseText) {
var res = responseText.split(",");
for (var i = 0; i < res.length; i++) {
var res2 = res[i].split("-");
if (res2[0] === "insert") {
alert(res2[0] + i);
}
}
}
When alert:
insert1 and insert2
Not begin for step 0.Please help.

if(res2[0]==="insert") should be if(res2[1]==="insert") because 'insert' will be the second element in res2 array.

It is Working....
var Text = "insert-90RS252,insert-90RS262,insert-90RS232";
var res = Text.split(",");
for (var i = 0; i < res.length; i++) {
var res2 = res[i].split("-");
if (res2[0].trim() === "insert") {
alert(res2[0] + i);
}
}

Related

how to count two string common character

*** i am try to this count two string match character ***
var s1 = "abcd";
var s2 = "aad";
function match(s1,s2){
var obj = {}
var spilt1 = s1.split("")
var spilt2 = s2.split("")
for(let i =0;i<spilt1.length;i++){
let th = spilt2.includes(spilt1[i])
if(!obj[th[i]]){
obj[th[i]] = 0
}else{
obj[th[i]]++
}
}
return obj
}
console.log(match(s1,s2))
*** output like this ***
{
a:1,
d:1
}
function match(str1, str2) {
const str1Obj = {};
const resultObj = {};
for (let i = 0; i < str1.length; i++) {
str1Obj[str1[i]] = str1Obj.hasOwnProperty(str1[i])
? str1Obj[str1[i]] + 1
: 1;
}
for (let i = 0; i < str2.length; i++) {
if (str1Obj.hasOwnProperty(str2[i]) && str1Obj[str2[i]] >= 1) {
resultObj[str2[i]] = resultObj.hasOwnProperty(str2[i])
? resultObj[str2[i]] + 1
: 1;
str1Obj[str2[i]] -= 1;
}
}
return resultObj;
}

I get the undefined in javascript

Can anyone expain why i get undefined?
function besked(input) {
let inputstring = String(input)
result = "";
for(i = 0; i < inputstring.length; i+=2 )
{
result += inputstring[i];
}
}
console.log(besked("HHeljw OirnFgaeCrs"))
the default return value in JS functions is undefined so in order to get something else from that function you should do it like so:
function besked(input) {
let inputstring = String(input)
result = "";
for(i = 0; i < inputstring.length; i+=2) {
result += inputstring[i];
}
return result;
}
console.log(besked("HHeljw OirnFgaeCrs"))

How to print the object like string in console.log in Javascript?

I want to print in the console the result from the function printNewObject(). How I can print with console.log in Javascript the result from the object? In the output I get the result Set {"1", "3", "4"}, but I want to look like a string, like 134.
Here is the code:
window.onload = function(){
inputBox = document.getElementById("myText");
btn = document.getElementById('sub');
inputBox2 = document.getElementById("myText2");
btn2 = document.getElementById('sub2');
btn.addEventListener("click",function(event){
event.preventDefault();
toObject(inputBox.value);
});
btn2.addEventListener("click",function(event){
event.preventDefault();
printNewObject(inputBox.value, inputBox2.value);
});
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
function printNewObject(rv, number) {
var mySet = new Set(rv);
for (var i = 0; i <= rv.length; i++) {
if (rv[i] == number) {
mySet.delete(rv[i]);
}
}
console.log(mySet);
}
}
function printNewObject(rv, number) {
var mySet = new Set(rv);
var st = "";
for (var i = 0; i <= rv.length; i++) {
if (rv[i] == number) {
mySet.delete(rv[i]);
}
}
var array = Array.from(mySet);
for (var i = 0; i < array.length; i++){
st += array[i];
}
console.log(st);
}

Push to an array in a nested loop is repeating the last value

I am trying to push elements to an array in a nested loop, but only the last item is getting repeated in the final array, where am I going wrong, very new to javascript asynchronous concept.Below is the function in which I push the items to an array.
$scope.showBeList = function(feed) {
if (feed[srcServ.KEY_CONTENT_TEXT]) {
var content = JSON.parse(feed[srcServ.KEY_CONTENT_TEXT])
if (content) {
$scope.beList = {};
for (var key in content) {
var decorationVal;
//reading value from a lokijs collection
var decoration = dataServ[srcServ.CONST_COLLECTION_DECORATION].find({
'name': key
});
if (decoration && decoration.length) {
decorationVal = decoration[0];
if (decorationVal != null) {
var tempObj = JSON.parse(decorationVal.value);
if (tempObj) {
var header = tempObj[key][key + '_HEADER'];
if (header) {
var counter = content[key].length;
var tempItems = [];
for (var j = 0; j < content[key].length; j++) {
(function(j) {
var obj = {};
obj[srcServ.KEY_MAIN_HEADER] = tempObj[key][srcServ.KEY_DESC];
obj[srcServ.KEY_SUB_HEADER] = header[srcServ.KEY_DESC];
obj.id = j;
var itemVal = content[key][j][key + '_HEADER'];
var details = [];
var notes = [];
for (var item in itemVal) {
var val = null;
var found = false;
for (var i = 0; i < header.field.length; i++) {
if (header.field[i].name == item) {
val = header.field[i];
found = true;
break;
}
}
if (found && val != null) {
val[srcServ.KEY_DESC_VALUE] = itemVal[item];
details.push(val);
}
}
obj.details = details;
counter--;
if (counter == 0) {
$scope.showMoreDetails = true;
$scope.beList.beItems = tempItems;
console.log(JSON.stringify($scope.beList));
}
tempItems.push(obj)
})(j);
// $scope.beList.beItems.push(obj);
}
}
}
}
}
}
}
}
}

JavaScript continue label scope

Map.prototype.updateMap = function (vehicles) {
nextVehicle:
for (var i = 0; i < vehicles.length; i++) {
for (var j = 0; j < this.oldVehicles.length; j++) {
var vehicle = vehicles[i];
var oldVehicle = this.oldVehicles[j];
if (vehicle.registration == oldVehicle.registration) {
oldVehicle.getPosition(function(latLng) {
if (vehicle.latitude != oldVehicle.lat) {
var newPos = new plugin.google.maps.LatLng(vehicle.latitude, vehicle.longitude);
oldVehicle.setPosition(newPos);
}
continue nextVehicle;
});
}
}
}
};
The code above does not work. I have a feeling this is to do with scope, I can't reach the nextVehicle label from inside the oldVehicle.getPosition method. How can I get around this?
Separate the matching logic from the update logic.
Map.prototype.updateMap = function (vehicles) {
// Only need to look up array lengths once
var vehiclesLength = vehicles.length,
oldVehiclesLength = this.oldVehicles.length;
for (var i = 0; i < vehiclesLength; i++) {
var vehicle = vehicles[i];
var oldVehicle = null;
// Find oldVehicle
for (var j = 0; j < oldVehiclesLength; j++) {
if (vehicle.registration == oldVehicle[j].registration) {
oldVehicle = oldVehicles[j];
break;
}
}
// Check for update if found
if (oldVehicle){
// Create closure for async callbacks
(function(oldV, lat,lng){
oldV.getPosition(function(latLng) {
if (lat != oldV.lat) {
var newPos = new plugin.google.maps.LatLng(lat,lng);
oldV.setPosition(newPos);
}
});
})(oldVehicle, vehicle.latitude, vehicle.longitude);
}
}
};
Just move the continue nextVehicle; line from inside the callback to immediately following the call to oldVehicle.getPosition(...):
Map.prototype.updateMap = function (vehicles) {
nextVehicle:
for (var i = 0; i < vehicles.length; i++) {
for (var j = 0; j < this.oldVehicles.length; j++) {
var vehicle = vehicles[i];
var oldVehicle = this.oldVehicles[j];
if (vehicle.registration == oldVehicle.registration) {
oldVehicle.getPosition(function(latLng) {
if (vehicle.latitude != oldVehicle.lat) {
var newPos = new plugin.google.maps.LatLng(vehicle.latitude, vehicle.longitude);
oldVehicle.setPosition(newPos);
}
});
continue nextVehicle;
}
}
}
};
This assumes the call to getPosition is a synchronous operation.
Edit:
Now if getPosition is asynchronous, you will need to use an asynchronous loop:
Something along this line might do the trick:
Map.prototype.updateMap = function (vehicles) {
var i = 0, j = -1, self = this;
var updatePosition = function() {
j++;
if (j == self.oldVehicles.length) {
j = 0;
i++;
}
if (i === vehicles.length) {
return; // We're done
}
var vehicle = vehicles[i];
var oldVehicle = self.oldVehicles[j];
if (vehicle.registration !== oldVehicle.registration) {
updatePosition();
}
else {
oldVehicle.getPosition(function(latLng) {
if (vehicle.latitude != oldVehicle.lat) {
var newPos = new plugin.google.maps.LatLng(vehicle.latitude, vehicle.longitude);
oldVehicle.setPosition(newPos);
updatePosition();
}
});
}
};
updatePosition();
};

Categories