Prototype in javascripts [closed] - javascript

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
How would I get my XCount to go to 100.
xCount==100;
var x= 'insert code here';
for (xCount = 1; xCount < 100 && x == x.constructor; xCount++)
x = x.constructor;

x has got a method constructor(), but not a property constructor. By setting x=x.constructor in your loop you avoid the exit condition of the loop. But the loop has already "finished before it was started" (i.e. it never ran) because the condition was not met when entering the loop!
If you look at x.construcor with console.log(x.constructor) you get something like
function String() {
[native code]
}
If you want the loop to run you must set
x=x.constructor
before the loop!

Related

Javascript Code explaination [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 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.

Find an element that has a text value of 0? [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 7 years ago.
Improve this question
I need to find an element that's text value is 0.
I do:
var d = $('.content').find('div');
if(d.text().length === 0){
//do something
}
Is there a way to put the above logic in the selector?
I've tried:
var d = $('.content').find('div:empty');
But no luck as the div may have other empty html tags in.
Use a filter
var elems = $('.content').find('div').filter(function() { return $(this).text() == 0 });

Simple Javascript validation function [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 do not have any JavaScript experience and would like some assistance in creating a function as I am not sure on how to do it.
I would like to create a Function that validates that x between 1 and 17 it can also be equal to 1 and 17
If the value is not valid a simple messagebox/alert could be used to notify the user
I know this is a really stupid question but thank you in advance
A function like that would look something like:
function validateNumber(n) {
var ok = n >= 1 && n <= 17;
if (!ok) alert('The value is not valid. Values from 1 to 17 are allowed.');
return ok;
}
You can call it, and you get the status back if you want to use it for something:
if (validateNumber(x)) {
// the number was valid
} else {
// the number wasn't valid
}

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

How to target a specific part of string? [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 9 years ago.
Improve this question
returns the index where pattern starts in string (or -1 if not found).
The search is to be case sensitive if the 3rd parameter is true otherwise it is case insensitive.
Examples
index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1
Maybe something like:
var str = "abAB12";
var i = str.indexOf("AB");
if (i < str.length / 2) {
//stuff
} else {
//other stuff
}

Categories