# can someone please show me how this is done

## Recommended Posts

looks simple but i cant get it: // this function uses the if blocks to // handle the optional parameters. // Change it to use the default operator instead. function sumValues(val1, val2, val3) { if (val1 === undefined) { val1 = 0; } if (val2 === undefined) { val2 = 0; } if (val3 === undefined) { val3 = 0; } return val1 + val2 + val3;}

##### Share on other sites

What happens when you use the function?

##### Share on other sites

What happens when you use the function?
3 values are going to be passed into the function and calculated. like : var addnums = sumValues(3, 5, 6) <-- sumValues is the name of the function and it returns addition... return val1 + val2 + val3;
##### Share on other sites

no, what literally happens when you run the function. are you checking for errors? are you debugging your function to test what is happening?

##### Share on other sites

im not sure. how the function would work wasnt clear. the whole code wasnt written. it was a task in a tutorial. the aim of what i was instructed to do is to learn to replace if statements with either ternary or default operators. they gave 1 example, then set this task. here's the example: if (min === undefined) {min = 1;}// becomes simplymin = min || 1; var contactInfo;if (email) {contactInfo = email;} else if (phone) {contactInfo = phone;} else if (streetAddress) {contactInfo = streetAddress;}// is greatly shortened tovar contactInfo = email || phone || streetAddress;

Edited by SnakesBite101
##### Share on other sites

I guess don't know what you are asking for or what the problem is.

##### Share on other sites

let me try to explain: function sumValues(val1, val2, val3) {if (val1 === undefined) {val1 = 0;} if (val2 === undefined) {val2 = 0;} if (val3 === undefined) {val3 = 0;}return val1 + val2 + val3;} the function is expecting 3 values(numbers) to be passed in for addition. like this, var addnums = sumValues(3, 5, 6) .. the IF statement means if a number is left out. like if i only pass val 1 and 2 but not 3. var addnums = sumValues(3, 5) <--- if val3 === undefined {value3 = 0}. they want me to use the default operator instead of the IF statement to set the value of any arguments that are left undefined to 0.

Edited by SnakesBite101
##### Share on other sites

Just to point out, these two are not equivalent:

if (min === undefined) {min = 1;}// becomes simply min = min || 1;
If min is 0, the first piece of code will not change it, the second one will. This will be equivalent: min = min === undefined ? 1 : min; Hopefully you can use that example to do the other replacements.
• 1
##### Share on other sites

thanks i will give it a try