Javascript Code explaination [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am a javascript beginner, and I do not quite understand this code in the picture, can somebody explain a little ?
Thanks!!

The easiest way to explain the code is to fill in the values during each call.
The function plusGenerator is a function that takes an offset and then returns another function that returns the result of adding offset to another number. When you call plusGenerator(2), the function that is returned looks like:
var addTwo = function(x) { return x + 2; };
You then call addTwo(15) which returns 17.

Related

String processing, get last string in vuejs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
i am dealing with strings in vuejs. Now I have 4 url ​​strings:
https://web-sand.com/product/slug/apple-iphone-13
https://web-sand.com/product/slug/samsung-galaxy
https://web-sand.com/product/slug/xiaomi-red
https://web-sand.com/product/slug/apple-ipad
Now I want to process to get the final string. Since my string is not fixed length using fixed ways is not efficient.
Result I want to get :
apple-iphone-13
samsung-galaxy
xiaomi-red
apple-ipad
Everyone please give me any comments, thanks.
You can use:
function getStr(str) {
return str.split('\/').pop()
}
Here:
input.split("\n").map(line => line.split("/").pop())
[
'https://web-sand.com/product/slug/apple-iphone-13',
'https://web-sand.com/product/slug/samsung-galaxy',
'https://web-sand.com/product/slug/xiaomi-red',
'https://web-sand.com/product/slug/apple-ipad'
].map(item => item.split('/').pop())

Write a function using native javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm not that good in javascript functions. Please help me understand below code.
var num = 5; // i have a variable 5
Add 10; //returns 15
Add 23; //returns 28
The question is, how to create a function that,
if add 10 it will return 15
if add 23 it will return 28
It seems you are looking for partial function in javascript which use the concept of closure.It is also known as currying-partials
var add = number(5);
function number(x) {
return function y(v) {
return x + v;
}
}
console.log(add(10));
console.log(add(23))

Returns the cube of the sum of an input and 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm taking online class to learn JS and I'm stuck. The quiz is asking me to:
Returns the cube of the sum of an input and 2.
So I coded this:
var returnCubed = function(num){ return (num + 2) * 3;};
But it doesn't pass. What else can I do to take in a parameter, add two to it, then cube it? Or am I not understanding the directions?
Try
var returnCubed = function(num){
return (num+2) * (num+2) * (num+2);
};
*3 will always multiply with 3.
Hope this can solve your problem.

Can someone explain to me why arguments within functions immediately get assigned the value of the object at hand? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the code below:
// Get Index of object based on id value
var arrayPosition = userListData.map(function (arrayItem) {
return arrayItem.username;
}).indexOf(thisUserName);
Why is it that arrayItem = userListData[0], userListData[1], userListData[2]... ?
In general, they do not.
That is just what the map function is designed to do.
callback —
Function that produces an element of the new Array, taking three arguments:
currentValue — The current element being processed in the array.
etc

In this function, (speed) is the parameter, and what does the zero (0) do there? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this.moveRight = function(speed) {
this.move(speed, 0);
};
I am new at Javascript, I just took this piece of code from one game, and I want to know what is happening with this function.
speed is the parameter to the moveRight function which you create with function().
It calls the move function internally on this with parameters speed and 0.
So moveRight is like a partial which provides a default parameter for move: (0) and retains the parameter speed.
0 probably stands for the right direction.

Categories