I am try to call a nesting function like below:
function public(val) {
if (val == "exist") {
function runList() {
for (var i = 0; i < arguments.length; i++) {
arguments[i]();
}
}
}
public.runList = runList;
}
var publicExist = "exist";
function test() {
console.log(test)
}
function testE() {
console.log(testE)
}
public(publicExist);
public.runList(test, testE);
I want to fire function, but these codes only show the text, how can I do?
You forgot to put quotes around 'test' and 'testE', which I am assuming you wanted to log to the console as strings.
function public(val) {
if (val == "exist") {
function runList() {
for (var i = 0; i < arguments.length; i++) {
arguments[i]();
}
}
}
public.runList = runList;
}
var publicExist = "exist";
function test() {
console.log('test')
}
function testE() {
console.log('testE')
}
public(publicExist);
public.runList(test, testE);
Just add quotes in test and testE method's console -
function public(val) {
if (val == "exist") {
function runList() {
for (var i = 0; i < arguments.length; i++) {
arguments[i]();
}
}
}
public.runList = runList;
}
var publicExist = "exist";
function test(){
console.log('test')
}
function testE(){
console.log('testE')
}
public(publicExist);
public.runList(test,testE);
Related
So I have a block of code like this:
var hello3 = [];
function testfunc3() {
let i = 0;
while (i < 10) {
hello3[i] = {
another3: function() {
return i;
}
};
i++;
}
}
function alertnow3() {
alert(hello3[6].another3());
}
When I call testfunc3 first, and then call alertnow3, the alert I am getting is "10"
Then I changed the code block to this:
var hello1 = [];
function testfunc1() {
for (let i = 0; i < 10; i++) {
hello1[i] = {
another1: function() {
return i;
}
};
}
}
function alertnow1() {
alert(hello1[6].another1());
}
Now when I call testfunc1 first, and then call alertnow1, the alert I am getting is "6"
Then I changed the code to this:
var hello2 = [];
function testfunc2() {
for (let i = 0; i < 10; ) {
hello2[i] = {
another2: function() {
return i;
}
};
i++;
}
}
function alertnow2() {
alert(hello2[6].another2());
}
Now when I call testfunc2 first, and then call alertnow2, the alert I am getting is "7"
Why are the responses different for the 3 blocks of code? I was expecting all responses to be 6.
Here is a codesandbox link for you guys to check (All 3 cases given above is demonstrated in the code. Click on "Click Here" first and then "Alert Now": https://codesandbox.io/s/nostalgic-microservice-mtl1t?file=/src/App.js:329-577
there is no output to the code even tho it is being called at the bottom. How can i fix this?
<script>
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (i = 0; i<List.length; i++){
if (List[i] = item) {
return true;
} else {
return false;
}
}
}
inList();
}
hasMatch(2);
hasMatch();
</script>
You need to return the output of inList
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (i = 0; i<List.length; i++){
if (List[i] = item) {
return true;
} else {
return false;
}
}
}
return inList();
}
console.log(hasMatch(2));
console.log(hasMatch())
Try to do like this:
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (var i = 0; i<List.length; i++){
if (List[i] == item) {
return true;
}
}
return false;
}
return inList(item);
}
console.log (hasMatch(2));
I'm currently stuck on a problem. I'm trying to make [[1,2,[3]],4] -> [1,2,3,4] but cannot get it to work. The output I keep getting is: 1,2,3,4
1,2,3
3
3
3
3..........3
function flattenArray(input) {
var result = [];
console.log(input.toString());
for(i = 0; i < input.length; i++) {
if(input[i].constructor === Array) {
result.push(flattenArray(input[i]));
} else {
result.push(input[i]);
}
}
return result;
}
console.log(flattenArray([[1,2,[3]],4]));
I have this in my common.js file. I use it all the time.
Array.prototype.flatten = function () {
var ret = [];
for (var i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
ret = ret.concat(this[i].flatten());
} else {
ret.push(this[i]);
}
}
return ret;
};
Here it is as a function:
function flattenArray(input) {
console.log(input.toString());
var ret = [];
for (var i = 0; i < input.length; i++) {
if (Array.isArray(input[i])) {
ret = ret.concat(flattenArray(input[i]));
} else {
ret.push(input[i]);
}
}
return ret;
}
extend is based on each function:
function each(collection,iterator) {
if (Array.isArray(collection)) {
for (var i=0; i < collection.length; i++) {
iterator(collection[i]);
}
} else {
for (var key in collection) {
iterator(collection[key]);
}
}
}
after I check underscore.js, apparently the extend function takes two parameters...but im not sure how to rewrite the function so it can work?
function extend(newProperty) {
each(arguments,function(source) {
each(source,function(value,key) {
newProperty[key]=value;
})
})
return newProperty;
}
var iceCream = {flavor: "chocolate"};
extend(iceCream,{sprinkles: "lots"});
//==> { flavor: 'chocolate', undefined: 'chocolate' }
Your each function is only providing the element of the collection, it should also provide the key. Try this.
function each(collection,iterator) {
if (Array.isArray(collection)) {
for (var i=0; i < collection.length; i++) {
iterator(collection[i], i);
}
} else {
for (var key in collection) {
iterator(collection[key], key);
}
}
}
I have a for loop in a function in the structure
func(var, callback) {
for(i = 0; i < len; i++) {
validate(var, function(value) {
if (!value) { callback(value) }
}
}
callback(true);
}
Where the function validate returns a boolean. I would only like to call my callback with true if it has not been called before. I tried putting a return after callback(value) but that didn't help.
Set a flag:
function func(foo, callback) {
var called = false;
for(var i = 0; i < len; i++) {
validate(foo, function(value) {
if (!value) {
called = true;
callback(value);
}
})
}
if (!called) {
callback(true);
}
}