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"))
Related
I have the following function.the code does what i need it to do,however its not structured correctly because of the declarations therefore it gives "(ESLint) move function to body root" error :
This is my code:
function onSuccess(result) {
if (result) {
function GetTotal(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
}
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
This is what i tried:
function onSuccess(result) {
var fn;
if (result) {
fn=function GetTotal(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
};
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
the "(ESLint) move function to body root" gets solved But with the above it returns an error saying "getTotal is not defined"
Then function declaration is on right hand side. It becomes a local variable to the variable on left hand side. Use the name GetTotal instead of fn in the main function scope.
function onSuccess(result) {
var GetTotal;
if (result) {
GetTotal = function(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
};
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
You can just move the function to the body root as suggested by ESLint like so
function onSuccess(result) {
function GetTotal(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
}
if (result) {
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
Or just get rid of the function as it's inside of a function anyway and you don't seem to be using it again.
function onSuccess(result) {
if (result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
var span = document.getElementById("Total");
span.innerHTML = str
}
}
On your second example the function is now inside the variable fn. You can access it by calling fn(). Another option is to rename the variable to reflect the function behavior and then call it by its new name.
var fn;
fn=function GetTotal(result) {
console.log('function called!');
}
fn();
Simgply add rules to disable the eslint rule. I.E. add "no-inner-declarations":[0, "always"], in .eslintrc.json
I am getting an error in console while i am trying to get a specific value from json data. This is the error:
'Uncaught TypeError: Cannot read property 'processen_id' of undefined'
Here is my code:
$.get("/getProces", function (data) {
if (data.error) {
} else {
for (var i = 0; i <= data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
}
})
}
This is what i get when i log (data):
You have a mistake in your code in the for loop
<= instead of <
$.get("/getProces", function (data) {
if (data.error) {
} else {
for (var i = 0; i < data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
}
})
}
The error message means that obj is undefined. That means, that data.message[i] gets an undefined value. The problem is the loop. You get an i that is larger then the array. Change <= to <:
for (var i = 0; i < data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
index out of range: for (var i = 0; i <= data.message.length; i++) { should be for (var i = 0; i < data.message.length; i++) { instead.
you can also optimize your code in this way:
$
.get("/getProces")
.then((res) => res.error ? Promise.reject(res) : Promise.resolve(res))
.then((data) => {
for (var i = 0; i < data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
})
module.filter('myCustomFilter', function ($filter) {
return function(items, searchedTxt, headers) {
if (headers.choice === "option1") {
return resultByDates(items, searchedTxt);
} else if (headers.choice === "option2") {
return resultByName(items, searchedTxt, headers);
}
else if (headers.choice === "option3") {
return resultSimple(items, searchedTxt);
}
return items;
};
function resultByDates(items, search) {
if (search === undefined || search === null)
return items;
var k = Object.keys(search)[0];
var i;
for (i = 0; i < items.length; i++) {
items[i][k] = $filter('date')(items[i][k], "MM/dd/yyyy");
}
var filteredData = $filter('filter')(items, search);
var indexes = [];
for (i = 0; i < filteredData.length; i++) {
indexes.push(items.indexOf(filteredData[i]));
}
var output = [];
for (i = 0; i < indexes.length; i++) {
output.push(items[indexes[i]]);
}
return output;
}
function resultByName(items, search, headers) {
if (search === undefined || search === null)
return items;
if (headers !== undefined) {
var k = Object.keys(search)[0];
var i;
var componentVals = headers.componentVals;
var itemsCopy = angular.copy(items);
for (i = 0; i < items.length; i++) {
for (var obj in componentVals) {
if (componentVals[obj].ID === itemsCopy[i][k]) {
itemsCopy[i][k] = componentVals[obj].Name;
break;
}
}
}
var filteredData = $filter('filter')(itemsCopy, search);
var indexes = [];
for (i = 0; i < filteredData.length; i++) {
indexes.push(itemsCopy.indexOf(filteredData[i]));
}
var output = [];
for (i = 0; i < indexes.length; i++) {
output.push(items[indexes[i]]);
}
return output;
}
return items;
}
function resultSimple(items, search) {
if (search === undefined || search === null)
return items;
return $filter('filter')(items, search);
}
});
Guys, I have above filter which works - partially, Option1 and Option3 returns correct filtered data, but there is some problem with Option2.
When I filter data with Option1 it returns correctly filtered data, then I can additionally filter with Option3 and it filters incorrectly returned previously data. When I use Option2 it seems like the data is being returned is not binded with the previous return, it's returning separate data.It seems like it is a separate collection... Is there something wrong with the way I return data in Option2?
Hope I have explained problem sufficiently. Thanks.
Below version with some fixes suggested by Himmel.
module.filter('myCustomFilter', function ($filter) {
return function (items, searchedTxt, headers) {
var key;
var i;
var indexes;
var filteredData;
var output;
if (headers.choice === "option1") {
key = Object.keys(searchedTxt)[0];
for (i = 0; i < items.length; i++) {
items[i][key] = $filter('date')(items[i][key], "MM/dd/yyyy");
}
filteredData = $filter('filter')(items, searchedTxt);
indexes = [];
for (i = 0; i < filteredData.length; i++) {
indexes.push(items.indexOf(filteredData[i]));
}
output = [];
for (i = 0; i < indexes.length; i++) {
output.push(items[indexes[i]]);
}
return output;
} else if (headers.choice === "option2") {
key = Object.keys(searchedTxt)[0];
var componentVals = headers.componentVals;
var itemsCopy = angular.copy(items);
for (i = 0; i < items.length; i++) {
for (var obj in componentVals) {
if (componentVals[obj].ID === itemsCopy[i][key]) {
itemsCopy[i][key] = componentVals[obj].Name;
break;
}
}
}
filteredData = $filter('filter')(itemsCopy, searchedTxt);
indexes = [];
for (i = 0; i < filteredData.length; i++) {
indexes.push(itemsCopy.indexOf(filteredData[i]));
}
output = [];
for (i = 0; i < indexes.length; i++) {
output.push(items[indexes[i]]);
}
return output;
} else if (headers.choice === "option3") {
return $filter('filter')(items, searchedTxt);
}
return items;
};
});
I have finally figured out the problem… So when I filtered with Option3 there was no issue because I didn’t modify collection before filtering, after when I filtered with Option1 I did mods to the collection because I wanted to filter based on displayed formatted date. Finally when I used Option3 I was only modifying collection for needs of current Option3 filter – without considering that before filtering I should consider mods that I have done during Option2 filtering. To confirm/test I have created a global array which holds my modified collection, so every time I’m filtering with any of the options I’m using that global array. It’s very dirty temporary solution but it works. Hope I clarified this problem enough. Is there a better solution?
It seems like you're trying to combine learning JavaScript and Angular at the same time, tough times!
module.filter('myCustomFilter', function ($filter) {
// You probably aren't trying to return "function", here
return function(items, searchedTxt, headers) {
if (headers.choice === "option1") {
return resultByDates(items, searchedTxt);
} else if (headers.choice === "option2") {
return resultByName(items, searchedTxt, headers);
}
else if (headers.choice === "option3") {
return resultSimple(items, searchedTxt);
}
return items;
};
// are you declaring another function after you've returned?
function resultByDates(items, search) {
if (search === undefined || search === null)
return items;
var k = Object.keys(search)[0];
var i;
for (i = 0; i < items.length; i++) {
items[i][k] = $filter('date')(items[i][k], "MM/dd/yyyy");
}
var filteredData = $filter('filter')(items, search);
var indexes = [];
for (i = 0; i < filteredData.length; i++) {
indexes.push(items.indexOf(filteredData[i]));
}
var output = [];
for (i = 0; i < indexes.length; i++) {
output.push(items[indexes[i]]);
}
return output;
}
// another??
function resultByName(items, search, headers) {
if (search === undefined || search === null)
return items;
if (headers !== undefined) {
var k = Object.keys(search)[0];
var i;
var componentVals = headers.componentVals;
var itemsCopy = angular.copy(items);
for (i = 0; i < items.length; i++) {
for (var obj in componentVals) {
if (componentVals[obj].ID === itemsCopy[i][k]) {
itemsCopy[i][k] = componentVals[obj].Name;
break;
}
}
}
var filteredData = $filter('filter')(itemsCopy, search);
var indexes = [];
for (i = 0; i < filteredData.length; i++) {
indexes.push(itemsCopy.indexOf(filteredData[i]));
}
var output = [];
for (i = 0; i < indexes.length; i++) {
output.push(items[indexes[i]]);
}
return output;
}
return items;
}
// christ!!
function resultSimple(items, search) {
if (search === undefined || search === null)
return items;
return $filter('filter')(items, search);
}
});
So, the first line, function($filter) {... is the function that is "invoked" when the list filter is triggered. The value that it returns will be the values that constitute the new list.
So if you "return" [1, 2, 3, 4] you will see those in the list in the browser. But if your function immediately returns a "function" instead of a "list" ([]), well then the browser will probably do something weird with it...
Oh wait, you're calling a bunch of functions that are outside of the current function you're in. Probably remove resultByDates from the current function call, as well as resultByName..., put them as siblings to module.filter.
This question is very hard to fix with so many issues, can you isolate a simpler problem? Maybe a small piece of unexpected behavior?
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);
}
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);
}
}