DEV Community

Chantae P.
Chantae P.

Posted on

My Solution to TOP Fundamentals Part 3 Problems

These are my answers that I came up with for The Odin's Project exercises using arrow functions.

  • 1. Write a function called add7 that takes one number and returns that number + 7.
const add7 = (num) => num + 7;
Enter fullscreen mode Exit fullscreen mode
  • 2. Write a function called multiply that takes 2 numbers and returns their product.
const multiply = (x, y) => x * y;
Enter fullscreen mode Exit fullscreen mode
  • 3. Write a function called capitalize that takes a string and returns that string with only the first letter capitalized. Make sure that it can take strings that are lowercase, UPPERCASE or BoTh.
const capitalize = (str) => {
let firstLetter = str.slice(0,1);
let restOfStr = str.slice(1,);
return `${firstLetter.toUpperCase()}${restOfStr}`;
}
Enter fullscreen mode Exit fullscreen mode
  • 4. Write a function called lastLetter that takes a string and returns the very last letter of that string.
const lastLetter = (str, n) => {
let theLastLetter = str.slice(-1,);
return theLastLetter;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
frankwisniewski profile image
Frank Wisniewski

ES6 arrow functions give you an alternative way to write shorter syntax compared to function expression.

Because of this:

const capitalize = str => str[0].toLocaleUpperCase() + str.slice(1)
const lastLetter = str => str.slice(-1)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
taepal467 profile image
Chantae P.

Agree. This is a good solution as well.