# JavaScript Arrow Function Exercises and Practice Questions

You will find practice questions related to the arrow function in JavaScript on this page. Solving these exercise questions will improve your understanding of different ways of using arrow functions in JavaScript.

If you are interested in solving questions related to the javascript array and object destructuring, then visit the following links:

Q1 Write an arrow function expression called `greet()`. It should accept a single argument representing a person's name. It should return a greeting string as shown below:

```greet("Hagrid"); //"Hey Hagrid!"
greet("Luna"); //"Hey Luna!"```

Solution

```const greet = (name) => {
return `Hey \${name}!`;
};

console.log(greet("Hagrid")); //"Hey Hagrid!"
console.log(greet("Luna")); //"Hey Luna!"```

Q2 Write an arrow function named `arrayAverage` that accepts an array of numbers and returns the average of those numbers.

Solution

```const arrayAverage = (arr) => {
let total = 0;
for(let number of arr){
total += number;
}
}

let ar = [1, 2, 3, 4, 5, 6];
console.log(arrayAverage(ar));```

Q3 Convert the following JavaScript function declaration to arrow function syntax.

```function counterFunc(counter) {
if (counter > 100) {
counter = 0;
}else {
counter++;
}

return counter;
}```

Solution

```const counterFunc = (counter) => {
if (counter > 100) {
counter = 0;
}else {
counter++;
}

return counter;
}```

Q4 Write an arrow function for the following JavaScript function:

```function nameAge(name, age) {
console.log("Hello " + name);
console.log("You are " + age + " years old");
}```

Solution

```const nameAge = (name, age) => {
console.log("Hello " + name);
console.log("You are " + age + " years old");
}```

Q5 Write an arrow function named `dashTwixt2Evens` that accepts a number and inserts dashes (-) between two even numbers.

Expected Output

```dashTwixt2Evens(225468) //"2-254-6-8"
dashTwixt2Evens(8675309) //"8-675309"```

Solution

```const dashTwixt2Evens = (number) => {

//convert number to array
let arr = [];
let temp = number;
let quotient, remainder;
while(temp!=0){
remainder = temp%10;
temp = Math.floor(temp/10);
arr.unshift(remainder);
}

//Add dash between two even numbers
let indexPosition = [];
for(let i=0; i<arr.length-1; i++){
if(arr[i]%2 === 0){
if(arr[i+1]%2 === 0){
arr.splice(i+1, 0, "-");
i=0
}
}
}
return arr.join("");
}```

Q6 Write an arrow function named `sumEvens` that accepts an array of numbers and returns the sum of the even numbers in the array. Use a `for...of` statement.

Solution

```const sumEvens = (arr) => {
let sum = 0;
for(let number of arr){
if(number%2===0){
sum += number;
}
}
return sum;
}

let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(sumEvens(numbers)); //20```

Q7 Square and sum the elements of this array using arrow functions and in 1 line of code. Then find the average of the array.

`let nums = [2, 4, 5];`

Solution

```let nums = [2, 4, 5];

let squaredAndSum = nums.reduce((acc, cur) => acc + cur*cur, 0);

let average = squaredAndSum/nums.length;
console.log(average);```

Q8 How to return an empty object from an arrow function in JavaScript?

Solution

```() => { return {};}
() => ({});```

Q9 The following array is given:

`let list=[1,2,3,4,5,6,7,8];`

Use the `map()` function with arrow notation => to multiply each number by 10 and return the result.

Solution

```let list=[1,2,3,4,5,6,7,8];

let result = list.map(number => number*10);
console.log(result);```

Q10 Write the arrow function for the following:

```function printOnly(){
console.log("printing");
}```

Solution

```const printOnly = () => {
console.log("printing");
}```

Q11 Rewrite the following three functions as arrow functions. Make sure to assign them to a `const` identifier.

```function createFullName(firstName, lastName) {
return firstName + " " + lastName;
}

function doubleNumber(number) {
return number * 2;
}

function getEvenNumbers(array) {
let evenNumbers = [];
for (let i of array) {
if (i % 2 === 0) {
evenNumbers.push(i);
}
}
return evenNumbers;
}```

Solution

```const createFullName = (firstName, lastName) => firstName + " " + lastName;

const doubleNumber = number => number * 2;

const getEvenNumbers = array => {
let evenNumbers = [];
for (let i of array) {
if (i % 2 === 0) {
evenNumbers.push(i);
}
}
return evenNumbers;
}```

Q12 Convert the function `isEven()` into an equivalent arrow function.

```function isEven(num){
return num%2 === 0;
}```

Solution

`let isEven = num => num%2 === 0;`

Q13 Q13 Write an arrow function named `greetByFullName`, which accepts two strings (firstName and lastName) and returns a personalized greeting that spans two output lines. Use a template literal to solve this exercise.

`alert(greetByFullName("Jille", "Bonnefemme"));`

Expected Output

```Hello Jille Bonnefemme!
Welcome to JavaScript!```

Solution

```const greetByFullName = (firstName, lastName) => {
return `Hello \${firstName} \${lastName}!\nWelcome to JavaScript!`;
}```

Q14 Write an arrow function that will take one parameter weight in Kg. This arrow function will convert Kg to Lbs. Formula is kg*2.2

• If LBS is > 150, then the function should return "obese"
• If LBS is between 100 to 150, the function should return "you are ok"
• If LBS is < 100, then the function should return "underweight"

Solution

```const weightInLbs = (weightInKg) => {
let lbs = weightInKg * 2.2;
if(lbs > 150){
return "obese";
}else if(lbs >= 100 && lbs <= 150){
return "you are ok";
}else{
return "underweight";
}
}```

Q15 Q15 Write an arrow function named `arrMax` that accepts an array of numbers and returns the largest number in the array. Use the Array.forEach method.

```arrMax ([2, 3, 5, 7, 9]) -> 9
arrMax( [6, 2, 4]) -> 6```

Solution

```const arrMax = (arr) => {
let max = arr;
arr.forEach((number) => {
if(number > max){
max = number;
}
});
return max;
}```