DEV Community

Chantae P.
Chantae P.

Posted on

4 1

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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay