how to change constructor value JS [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
I want to change constructor base value like I have:
function G()
{
this.speed=1;
}
and var k=new G(); gives me k.speed=1;
now I want that every time I create new G , its speed was like 10;
I tried
G.changeSpeed=function(){this.speed=10;}
G.prototype.changeSpeed=function(){this.speed=10;}
second works on already initialised ones, but first doesn't work at all ( error ).
any way I can do it?

How about:
function G(speed)
{
this.speed = speed;
}
You can do:
var x = new G(1); // x.speed = 1;
var y = new G(2); // y.speed = 2;

Related

How to separate a value from a class [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 1 year ago.
Improve this question
I want to attempt to get a value from a site and calculate it.
At the moment when entering the command
document.getElementById("txtTehtav")
I recieve the answer along the lines of
<a class="thrida" id="txtTehtav" name="txtTehtav">3x3=</a>
My goal would be to separate the value (3x3) into X and Y (X x Y)
How would I be able to accomplish that?
Any help is appreciated.
const textValue = document.getElementById("txtTehtav").innerText; // Will return 3x3=
const [x, y] = textValue.substr(0, textValue.length - 1).split('x').map((n) => parseInt(n))
const muliplication = x*y;
Try this, you will get the text
document.getElementById("txtTehtav").textContent

Can i convert math.js object into JavaScript function? [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 1 year ago.
Improve this question
I'm developing simple js neural network and need to create javascript function (derivative of entered by user) before learning starts.
I know about evaluate(), but i think it'll be slowly than simple function.
That is what i want:
const derivative = math.derivative('x^2/sin(2x)', 'x');
const derivative_func = derivative.please_stay_func();
...
while(1) alert(derivate_func(12)) //:) alert result of (2 * 12 * Math.sin(2*12) - 2 * Math.pow(12, 2) * Math.cos(2*12)) / Math.pow(sin(2*12), 2)
Is it real? And maybe i'm not right about speed. Maybe there is some better ways - write here.
i am on mobile, please edit this
maybe this is want you want to do
const countThings = (customValue) ==>{
let x = 'x^2/sin(x)';
let y = 'x';
x = math.parse(x)
y = math.parse(y)
return math.derivative(x, y).evaluate({x:customValue});}
console.log(countThings(12))
and don't do while(1) alert because it will keep alerting

How can I make a variable static in Js? [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 2 years ago.
Improve this question
I want the newid to be incremented when I call the function.
You make the function close over the variable, by declaring the variable in the surrounding context:
let newid = 3;
const generateTemplate = contact => {
// ...
newid++;
};

For loop JS for a calculator [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 5 years ago.
Improve this question
I'm trying to create a calculator which goes through 18 units. I wanted to make my code shorter by using a for loop. I thought something like this would work:
var i=0;
for (i=0;i<=18;i++)
{
if (Unit[i] = "P" or Unit[i] == "p")
{
UnitTotal[i] = 70;
SetCookie('UnitAns'[i],UnitAns[i]);
}
}
This doesn't work what am I doing wrong or what do I need to do differently?
Unit[i] = "P"
Unless it throws an exception because Unit isn't defined, this will always be true. = is an assignment, not a comparison.
or
or is not a keyword in JavaScript. The OR operator is ||.

how can I call an unnamed instance in javascript? [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
So if I make a class, and then create new instances of that class without naming them -maybe with a loop that creates a bunch of instances- how can I call a specific (or unspecific) instance? For example, if I'm generating a bunch a squares, but I want to move a specific one somewhere, how would I do that?
Sorry if this is a total noob question, or if I miss-used some terminology, but I'm pretty new to programming.
Example code:
function example(x){
this.x = x;
}
for(var i=0; i<10; i++){
new example(1);
}
//now how would I get a specific instance of examples to have x = say, 10.
You could put each square in an array and access them that way:
function Square(i){
this.index = i;
}
Square.prototype = {
constructor: Square,
intro: function(){
console.log("I'm square number "+this.index);
}
}
var squares = [];
for(var i = 0;i < 10;i++){
squares.push(new Square(i));
}
squares.forEach(function(square){
// do something with each square
square.intro();
});
Demo: http://jsfiddle.net/louisbros/MpcrT/1/

Categories

Resources