diff --git a/curriculum/challenges/english/blocks/lecture-understanding-functional-programming/6733b0451d6be0065430b418.md b/curriculum/challenges/english/blocks/lecture-understanding-functional-programming/6733b0451d6be0065430b418.md index 68311c3f093..ecd98ad2bc6 100644 --- a/curriculum/challenges/english/blocks/lecture-understanding-functional-programming/6733b0451d6be0065430b418.md +++ b/curriculum/challenges/english/blocks/lecture-understanding-functional-programming/6733b0451d6be0065430b418.md @@ -5,7 +5,7 @@ challengeType: 19 dashedName: what-is-functional-programming --- -# --description-- +# --interactive-- Functional programming is about writing code that is clean, predictable, and easier to test and debug. Two key concepts we'll explore today are pure functions and avoiding side effects. @@ -47,16 +47,24 @@ While writing code, our main goal should be to make functions do one thing and t Here's an example of a function with a side effect: +:::interactive_editor + ```js function greet(name) { console.log(`Hello, ${name}!`); } + +greet("Alice"); ``` +::: + This function has a side effect because it interacts with the outside world by logging to the console. In functional programming, we might rewrite this as: +:::interactive_editor + ```js function greet(name) { return `Hello, ${name}!`; @@ -64,6 +72,8 @@ function greet(name) { console.log(greet("Alice")); ``` +::: + Now `greet` is a pure function that simply returns a string, and we handle the side effect (logging to the console) separately. Functional programming encourages us to compose our programs using pure functions by breaking our code into small, predictable pieces. And thus, we can build programs that are easier to understand, test, and maintain.