You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'use strict';functionadd(a,b){letaValue=a!==undefined ? a: 0;letbValue=b!==undefined ? b: 0returnaValue+bValue;}console.log(add(10,20))console.log(add())// both a and b are undefinedconsole.log(add(10))// b is undefinedconsole.log(add(undefined,10))// a is undefined
With Default Parameters
// ES6 - with default parameters'use strict';functionadd(a=0,b=0){returna+b;}console.log(add(10,20))console.log(add())// both a and b are undefinedconsole.log(add(10))// b is undefinedconsole.log(add(undefined,10))// a is undefined
Without Rest Parameters
'use strict';functionuserAccounts(userName,socialAccount1,socialAccount2){letmessage=userName+' has 2 social accounts - '+socialAccount1+', '+socialAccount2;console.log(message);}userAccounts("Sam","Facebook","Twitter");userAccounts("Amy","LinkedIn","Reddit");
With Rest Parameters
'use strict';functionuserAccounts(userName, ...socialAccounts){letallUserAccounts='';for(letindex=0;index<socialAccounts.length;index++){allUserAccounts+=socialAccounts[index]+', ';}console.log(userName+' has '+socialAccounts.length+' social accounts - '+allUserAccounts);}userAccounts("Sam","Facebook","Twitter");userAccounts("Amy","LinkedIn","Reddit");userAccounts("Cindy","Facebook");userAccounts("Dan","Facebook","LinkedIn","Twitter","Reddit");
Rest Parameters Error
'use strict';functionuserAccounts(...socialAccounts,userName){letallUserAccounts='';for(letindex=0;index<socialAccounts.length;index++){allUserAccounts+=socialAccounts[index]+', ';}console.log(userName+' has '+socialAccounts.length+' social accounts - '+allUserAccounts);}userAccounts("Sam","Facebook","Twitter");
functionexecFunction(callerName,callerAge,func){console.log('Caller Name: '+callerName);console.log('Caller Age: '+callerAge);func();}constf=()=>console.log('I was passed as an argument!!!');execFunction("James",42,f);
Write a function that multiplies 3 numbers. Write using function declaration, named function expression, anonymous function and arrow function
Change this function implementation such that if the user does not provide any input, the function does not return undefined. Rather, it uses 1 as a reasonable default for product.
Write a function called greet that takes 1 parameter called name. If the name is empty, it return "Hello Anonymous" else it returns "Hello" with the name appeneded. Write all form of functions - Function Declaration, Named Function Expression, Anonymouns Function, Arraor Function
Fix the following function so that it only add numbers. For now, it adds anything that it wants. It should return NaN if the inputs cannot be converted to a Number.
Write a function that takes a userName and the list of socialAccounts. The number of socialAccounts must be at least 1. If there are no socialAccounts, throw a new Error with a message "You must give at least 1 account". Otherwise, print the user name and the list of socialAccounts separated by comma.
Write a function called addUntil that takes an input number called num. Then add the numbers from 1 all the way up to the num. Then, finally return the result to the caller. If the function receives anything other than a number, it should throw an error - "You must provide a valid number".
Write a function called addEvenOdd that takes an input number called num. Then it calculates 2 sums
- First one called valueOfoddSum, which adds all the odd numbers from 1 until num.
- Second one called valueOfEvenSum, which adds all the odd numbers from 1 until num.
The function addEvenOdd, then, returns an object {oddSum: valueOfOddSum, evenSum: valueOfEvenSum}.