JavaScript function default parameters [duplicate] - javascript

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))

Related

Array.reduce() not working as expected, returns 0 [duplicate]

This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
console.log() async or sync?
(3 answers)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 4 months ago.
hello! I'm trying to add numbers from an array. I went with the .reduce method, however it's not working. It keeps giving me a result of 0. I console logged the array, and indeed the values are there. Why isn't it doing the addition then?
const cubitMeters =
(parseInt(height) * parseInt(width) * parseInt(length) * parseInt(count)) / 1000000
totalW.push(cubitMeters)
const totalW = []
const sum = totalW.reduce((a, b) => a + b, 0)
console.log('sum: ' + sum)
console.log(totalW)
and here's screenshot of console.log results
Looks like you need to move the array declaration to top. And then perform the push operation.
const totalW = []
const cubitMeters =
(parseInt(height) * parseInt(width) * parseInt(length) * parseInt(count)) / 1000000
totalW.push(cubitMeters)
const sum = totalW.reduce((a, b) => a + b)
console.log('sum: ' + sum)
console.log(totalW)

JavaScript function

function hi(a, b) {
return a * b
}
function hello(a, b) {
return hi(a, b + 2)
}
console.log(hello(2, 3)); // 10
I do not understand how the answer is 10. Would someone be able to help me?
function hi(a, b) {
return a * b
}
function hello(a, b) {
return hi(a, b + 2)
}
hello(2, 3)
//10*
I strongly believe, the * you used in the start is an error so the above is the correct beautified code
You are calling function hello with 2 arguments, 2 & 3 which calls hi function with argument (2, 5) as 3 + 2 is 5
When it will call hi, it will multiple 5*2 which is 10
Let's first you understand what you have written by yourself. There are two functions hi & hello
function hi(a, b) {
return a * b
}
function hello(a, b) {
return hi(a, b + 2)
}
hello(2, 3)
When the following function is executed
hello(2,3) returns hi(2,3+2).
hi(2,5) returns 2*5. Thus the answer is 10.
When you call the function with argument 2 and 3. These values get assigned to respective parameters.
a = 2 and b = 3
then inside the function hello you are returning the value of what hi function returns with arguments as
hi(a, b + 2)
// here what you've passed argument to hi is as follows:
// a => value of a => 2
// b => value of b + 2 => 3 + 2 => 5
so when hi executes it returns
a * b
2 * 5 = 10

Ternary operator not working properly in JS [duplicate]

This question already has answers here:
Operator precedence with JavaScript's ternary operator
(7 answers)
Ternary operation JS not working
(3 answers)
Closed 3 years ago.
I am running the following code and getting unexpected results:
var a = 1, b =2 ,c = 3;
console.log(5*a+ b>0?b:c);
Expected result was : 7 but getting 2.
Your code has the right concept, but wrong execution. The ternary is doing its job properly.
At the moment, your code is executing like this:
const a = 1
const b = 2
const c = 3
// This will evaluate to true, since 5 * 1 + 2 = 7, and 7 is greater than 0
if (5 * a + b > 0) {
// So return b
console.log(b)
} else {
console.log(c)
}
You should use brackets to separate the ternary:
const a = 1
const b = 2
const c = 3
console.log(5 * a + (b > 0 ? b : c));

How to right a variable length currying function or a general currying function for all type of inputs? [duplicate]

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.

Javascript parseFloat issue [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I am facing a new problem. I have the below use case.
var a = parseFloat(10); // Returns 10
var b = parseFloat(1.62); // Returns 1.62
var c = a + b; // Returns 11.620000000000001
I want the c to return 11.62. What is the problem here?
var a = parseFloat(10); // Returns 10
var b = parseFloat(1.62); // Returns 1.62
var c = a + b; // Returns 11.620000000000001
console.log(c.toFixed(2))
Output: 11.62
console.log(c.toPrecision(4))
Output: 11.62
var a = parseFloat(10); // Returns 10
var b = parseFloat(1.62); // Returns 1.62
var c = a + b; // Returns 11.620000000000001
console.log(c.toPrecision(4));
Output: 11.62

Categories

Resources