This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
I want to get two values out from the function for a specific condition (on "As_cm" values) and execute some operation on those values. I could not do that.
function steelSection() {
if (As_cm <= 29.2) {
return [D = 152.4, B = 152.2];
} else if (As_cm <= 38.3) {
return {D = 157.6, B = 152.9];
} else {
return [D = 1000, B = 2000];
}
}
var d = D / 2;
var b = B / 2;
console.log(d);
document.getElementById("flangeWidth").innerHTML = d ;
console.log(b);
document.getElementById("depth").innerHTML = b ;
expected values for As_cm <= 29.2
d= 152.4 / 2
b= 152.2 / 2
The error message I get is
Uncaught TypeError: Cannot set property 'innerHTML' of null
at steelcolumn.js:68
You could return an object and then take the destructured properties as values.
function steelSection() {
if (As_cm <= 29.2) return { d: 152.4, b: 152.2 };
if (As_cm <= 38.3) return { d: 157.6, b: 152.9 };
return { d: 1000, b: 2000 };
}
var { d, b } = steelSection();
document.getElementById("flangeWidth").innerHTML = d / 2;
document.getElementById("depth").innerHTML = b / 2;
Related
This question already has answers here:
Skip arguments in a JavaScript function
(9 answers)
Passing values to function with multiple default parameters
(1 answer)
Default parameter of a JavaScript function is not working when intermittent value is missing
(1 answer)
Pass only the second argument in javascript
(4 answers)
Omit/Skip a parameter with default value while calling a function in JS
(2 answers)
Closed last year.
const add = (a = 1, b = 1, c = 1) => a + b + c
add(4, , 2)
Throws Uncaught SyntaxError, unexpected token ','
How do I call the function so b defaults to the value 1
Just take undefined as value.
const add = (a = 1, b = 1, c = 1) => a + b + c
console.log(add(4, undefined, 2));
Pass undefined as value
Check this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters#passing_undefined_vs._other_falsy_values
const add = (a = 1, b = 1, c = 1) => a + b + c
console.log(add(4, undefined, 2))
This question already has answers here:
Create function to add such that add(1,2)(3,...k)(1,2,3)...(n) should sum all numbers [duplicate]
(1 answer)
"add" function that works with different combinations of chaining/arguments
(4 answers)
Closed 3 years ago.
function currying(func) {
//I need to complete this for all diiferent forms of add
}
//1st form
const add = currying(function (a, b) {
return a + b;
})
add(1, 2) //should yield 3
add(1)(2) //should yield 3
//second form
const add = currying(function (a, b, c) {
return a + b + c;
})
add(1, 2)(3) //should yield 6
add(1)(2)(3) //should yield 6
add(1, 2, 3) //should yield 6
//third form
const add = currying(function (a, b, c, d) {
return a + b + c + d;
})
const a11 = add(1)
a11(2)(3)(4) //should yield 9
a11(2, 3, 4) //should yield 9
How to complete the top most "currying" function for all these cases? "Currying" function must return correct answer for any of these kind of function calls.
This question already has answers here:
Official information on `arguments` in ES6 Arrow functions?
(2 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 5 years ago.
I have the following code
function coroutine(g) {
var it = g();
return () => it.next.apply(it, arguments);
// also tried () => { return it.next.apply(it, arguments);
}
var run = coroutine(function* () {
var x = 1 + (yield);
var y = 1 + (yield);
yield (x + y);
});
And the following testing has been executed.
run()
{value: undefined, done: false}
run(10)
{value: undefined, done: false}
run(30).value
"1function* () { var x = 1 + (yield); var y = 1 + (yield); yield (x + y);}1function* () { var x = 1 + (yield); var y = 1 + (yield); yield (x + y);}"
However, shouldn't the last run(30).value returns the value of 42?
However, it works after I change the arrow function to old anonymous function?
function coroutine(g) {
var it = g();
return function() { return it.next.apply(it, arguments) };
}
This question already has answers here:
Changing the interval of SetInterval while it's running
(17 answers)
Closed 5 years ago.
I can't seem to figure out why the farmerTime is not updating when you level up. There is a button that just adds a level to farmingLevel.
window.setInterval(function() {
farmerTime = 2500;
farmerLevel = 3;
x = farmerTime;
y = farmerLevel;
z = x / y;
farmerTime = z;
if (farmers >= 1) {
a = farmers;
b = potatoes;
c = a * 1;
d = b + c;
potatoes = d;
}
}, farmerTime);`
You need to define farmerTime before you use it. In your case, before the setInterval function. Also, if you want to change the farmerLevel you need to change it somewhere else, not in the setinterval function.
Changing level example:
<button type="button" onclick="setFarmerLevel(farmerLevel + 1);">Change level </button>
And the code for the interval thing:
var farmerTime = 2500;
var farmerLevel = 1;
var setFarmerLevel = function (level) {
farmerLevel = !level ? 1 : level;
farmerTime = farmerTime / farmerLevel;
clearInterval(farmerInterval);
farmerInterval = window.setInterval(run, farmerTime);
};
var run = function () {
if (farmers >= 1) {
a = farmers;
b = potatoes;
c = a * 1;
d = b + c;
potatoes = d;
}
};
var farmerInterval = window.setInterval(run, farmerTime);
UPDATE
I forgot setInterval's function time cannot be change in runtime, so the code is updated now.
That's a code fragment task - you should enter "var" (as many as want) in it in order to get 17 in the first, and 21 in the second alert. I thing that I have met this before, but still was not able to solve the issue.
a = 3;
b = 2;
function line(x) {
a = 5;
b = 4;
return a*x + b
}
//b should be 17
b = line( a ) - b;
alert( b );
//c should be 21
c = line ( a ) + b;
alert(c);
If you put "var" in the function in front of b, it will alert "17". The next alert gives us 46 because of the new value of b, return by the function.
function line(x) {
a = 5;
var b = 4;
return a*x + b
}
That's the source of the task:
http://www.codecademy.com/courses/javascript-for-jquery/1?curriculum_id=4fc3018f74258b0003001f0f/#!/exercises/3
Using exactly what's given, in exactly the way it's given is impossible.
What I mean by that is if the call:
c = line(a) + b;
is dependent upon the value of b which is the assignment at:
b = line(a) - b;
Then it's 100% impossible to either have made a a significantly-small number, or made b a significantly-large negative number to make the math work.
Therefore it's my belief that they're intended to be two separate checks.
Best-case scenario, if we're trying to have b=17 included:
a = 3;
3 * 5 = 15 + 4 = 19 + 4 = 23;
That's the smallest you're going to get, assuming you run the two back-to-back.
Even if you did it that way, you wouldn't get b = line(a) - b = 17 on the first run...
If it was written:
c = line(a) - b;
d = line(a) + b;
Then you could run both in succession and get the expected result.
Or you can run:
var a = 3,
b = 2;
function line (x) {
var a = 5,
b = 4;
return a*x + b;
}
b = line(a) - b;
and get 17.
Then you can run:
var a = 3,
b = 2;
function line (x) {
var a = 5,
b = 4;
return a*x + b;
}
c = line(a) + b;
(ie: the exact same setup with a different instigator, and without the saved b value from the return of the previous call), and get the desired result.
But it's not possible to run both of them one after the other, and expect to have them both work, without doing anything to the code but add a var or four.
Keep your function like this, if you want to maintain consisitency. Using "var" before a and b will make them local to the function block and that call. Otherwise they will refer to the global variable.
function line(x) {
var a = 5;
var b = 4;
return a*x + b
}