Skip to content

Commit 3554c30

Browse files
committed
Add more post source code
1 parent 231e70d commit 3554c30

8 files changed

+69
-0
lines changed

external_file.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function charactersCounter(text) {
2+
return `Character count: ${text.length}`;
3+
}
4+
5+
function analyzeFile(filename) {
6+
let fileContent = open(filename);
7+
return charactersCounter(fileContent);
8+
}

increment_numbers.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let list = [1, 2, 3, 4, 5];
2+
3+
function incrementNumbers(list) {
4+
return list.map(number => number + 1);
5+
}
6+
7+
console.log(incrementNumbers(list));

modifying_global_variable.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let counter = 1;
2+
3+
function increaseCounter(value) {
4+
counter = value + 1;
5+
}
6+
7+
increaseCounter(counter);
8+
console.log(counter); // 2

pure_function.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const PI = 3.14;
2+
3+
function calculateArea(radius, pi) {
4+
return radius * radius * pi;
5+
}
6+
7+
calculateArea(10, PI); // returns 314.0

random_number_generator.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function yearEndEvaluation() {
2+
if (Math.random() > 0.5) {
3+
return "You get a raise!";
4+
} else {
5+
return "Better luck next year!";
6+
}
7+
}

recursive_sum.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let list = [1, 2, 3, 4, 5];
2+
let accumulator = 0;
3+
4+
function sum(list, accumulator) {
5+
if (list.length == 0) {
6+
return accumulator;
7+
}
8+
9+
return sum(list.slice(1), accumulator + list[0]);
10+
}
11+
12+
console.log(sum(list, accumulator)); // 15
13+
console.log(list); // [1, 2, 3, 4, 5]
14+
console.log(accumulator); // 0

returns_increased_global_variable.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let counter = 1;
2+
3+
function increaseCounter(value) {
4+
return value + 1;
5+
}
6+
7+
console.log(increaseCounter(counter)); // 2
8+
console.log(counter); // 1

slugify_composition.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let string = " I will be a url slug ";
2+
3+
function slugify(string) {
4+
return string.toLowerCase()
5+
.trim()
6+
.split(" ")
7+
.join("-");
8+
}
9+
10+
console.log(slugify(string));

0 commit comments

Comments
 (0)