avoid .slice is not a function (javascript) - javascript

so i have a variable which as this point has a static definition, i want it to get now the value dynamically from an if-else statement but the above mentioned error pops out:
var x = [{a:b},{c:d},{e:f}]
and i want to change it to get the value from a function:
var x = function(){
if(true){
return [{a:b},{c:d},{e:f}]
}else{
return [{f:g},{h:i}]
}
}
But I get an error x.slice is not a function
Can what am i doing wrong? Looked around but cant manage to fix this... isn't it possible?

x is a function, so you're trying to call .slice() on a function. You want to call x() and use the return value, then slice that:
x().slice(1);

x is a function so it does not have slice you should run it before
var x = (function(){
if(true){
return [{a:b},{c:d},{e:f}]
}else{
return [{f:g},{h:i}]
}
})()

Related

What does Return do in here?

I have problem with 'return' means in this code.
1.
function func4() {
var str = "function works.";
console.log(str);
}
func4();
2.
function func4() {
var str = "function works.";
return str;
}
var value = func4();
console.log(value);
Both of them, their result is 'function works.'.
I know that return used for exit function but I'm still confuse when I have to use return exactly.
Sorry about my super basic question :(
As far as I understand 'return' assigns value to a function and returns it, so you're displaying function's value. In the first case you are just simply invoking a function to display a string.
Let's analize this two scenarios:
You have a function that initialize a variable with a predefinided value, and then, you log the value. Then, outside the function you execute it
You have the same variable but with the difference that instead of loggin the value inside the function, you returned it from it. So you can initialize the funcion and store the value on another variable var value = func4();.
Let me try to explain it with some requirements.
I want a function which returns me some data instead of passing a variable and updating the variable in function.
You call a function and it is always best to test negative scenarios first. So in case of negative scenario you can return it from there it self.
In your second case if you see you are getting a value from that function and then printing it. Same thing you can not do using first function.
Always there are workarounds for everything. In the end it depends on your need and what is best suited for that situation.
Both of those functions don't equal the same thing, but they do log the same string.
func4() in #1 is equal to undefined, because it returns nothing.
func4() in #2 returns (gives back) the value "function works.", a string, which is then given to console.log outside of the function.
function func1() {
var str = "function works.";
// console.log(str);
}
func1();
function func2() {
var str = "function works.";
return str;
}
// console.log(func2());
console.log(func1() === undefined);
console.log(func2() === 'function works.');
If you want to use the func4() value for further calculations without calling it again, then you would return {value}.
For e.g
function func4(userInput) {
return userInput % 2 == 0;
}
var response = func4(userInput);
if(response == true) {
console.log('user entered an even number');
} else {
console.log('user entered a odd number');
}
// from here you can use the value of response n times without calling the function again.
Whereas, if you don't return then you will have to call the function x number of times whenever you want to re-user the response of it.
function func4(){
var str = "function works.";
return str;
}
var value = func4();
console.log(value);
//here return means you are returning the value of variable 'str'.
You can find the details here.
https://learn.microsoft.com/en-us/cpp/c-language/return-statement-c?view=vs-2019#:~:text=A%20return%20statement%20ends%20the,value%20to%20the%20calling%20function

Getting Value from Function in Javascript

I am a beginner at Javascript and am running into a little problem. It is a homework problem, but it is graded based on completion only, so I am only trying to figure out the right answer for myself.
I am supposed to define a function, repeatUntil, that takes in two other functions, say f(returns a number) and g (returns a boolean value). The functionality of repeatUntil is to repeat function f at least once until g returns true.
Here is what I have so far:
function repeatUntil(f, cond) {
var f1;
do{
f1 = f;
return f1;
}
while(cond(f1()));
}
And here is the tester/how we call it:
var print = console.log;
var r = repeatUntil(function(x) { return x + x }, function(x) { return x >= 20 })
print(r(2))
print("Expected: 32")
The function runs, but my problem right now is storing the updated value of x from the repeatUntil function. Right now the function only runs once, and the condition is not updated because I cannot pass in the updated value of x into the function g. I tried putting the result of f() into a variable, but it will only return a function and not a number.
Any help/advice would be greatly appreciated. Thank you!
Combining the existing comments into an answer.
Since you need to call r(), your function needs to return another function.
The do loop will run exactly once because you return in the body of the loop
Code
function repeatUntil(f, cond) {
return function() {
while(cond(f()));
}
}

alert function in javaScript

Can someone help me to better understand why the second alert box appears with 'undefined' in it? When I call the function without alert function I don't see it.
var n = 1; // global scope
function one() {
alert(n);
}
alert(one());
alert(one()) alerts the return value of the function one. That function does not return a value, so its return value is undefined.
If you want to change the value of the second alert, you need to use return <value> from within one.
var n = 1; // global scope
function one() {
alert(n);
return 42;
}
alert(one());

Watching a function

In javascript I know we can watch for a property change, but is it possible to watch a function?
If I had a function that evaluates to a boolen
function eval(value) {
return value == 1;
}
Would it be possible to continuously evaluate this function until true?
I know setTimeout could work, but that seems like a hack. Is there a better way?
There are two main possible ways, the first is yours, the setTimeout. The second is a setInterval:
var checkEval = setInterval(function(){eval(value)},1000);
And you could end it using:
clearInterval(checkEval);
replace the function with a wrapper
function something(){
return value==1;
}
(function(window){
var oldSomething = window.something;
window.something = function(){
var result = oldSomething.apply(null,arguments);
if(result===true){
//do what you need
}
return result;
};
})(window);

Proper syntax to return variable and use it again (javascript)

I have this very simple thing, that doesn't work. What is happening? According to the tutorials that I have read, this should output 4...
function sum(a,b) {
var result = a + b;
return result;
}
sum(2,2);
var test = sum();
alert(test); // shouldn't this return "4"?
Link to the JSFiddle
function sum(a,b) {
var result = a + b;
return result;
}
var test = sum(2,2);
alert(test);
Change this:
sum(2,2);
var test = sum();
To this:
var test = sum(2,2);
The first code isn't technically wrong it just isn't doing what you're trying to do. You're calling the sum function with the appropriate values but never setting it's return value to any variable so it just gets thrown away. You seem to be under the impression that the value will "stick" to the function and this isn't the case. (Some BASIC languages can make it seem this way though. Perhaps that's where your misconception is coming from.)
Your second call is essentially the equivalent of
var test = sum(null, null);
and when you concatenate two null values you get null again.

Categories