Upping my javascript game

I've been developing for web (professionally) for more than 10 years.

But because my roots were more communication and design I haven't had a proper computer engineering education.
As I always say, I miss the programming 101's.

Already have I dabbled with Typescript, Angular, Vue, PHP and even a bit Python. So I can program a bit.
But this year I want to focus and streamlining my Javascript skills. Perhaps even a good Node.js or Deno app.

My end goal this year will be a fancy Javascript game. I like gaming so this would be extra fun.

I will read the following book and put all exercises on this post.

Eloquent Javascript


15%

This book has recently had an upgrade (march 2024 4th ed.), I actually started this while studying but never finished it. So now I will!
Started reading this April 1st. No joke. :)

Exersises Chapter 02

What follows are my solutions.

// https://eloquentjavascript.net/02_program_structure.html#i-umoXp9u0e7
function triangle() {
let triangle = '';
while(triangle.length < 7) {
triangle += '#';
console.log(triangle);
}
}
triangle();
// https://eloquentjavascript.net/02_program_structure.html#i-umoXp9u0e7
function fizzBuzz() {
for (let number = 1; number < 101; number++) {
if (number % 3 === 0 && number % 5 === 0) {
console.log("FizzBuzz");
} else {
if (number % 3) console.log("Fizz");
else if (number % 5) console.log("Buzz")
else console.log(number);
}
}
}
fizzBuzz();
// Chessboard
// https://eloquentjavascript.net/02_program_structure.html#i-swb9JBtSQQ
function generateBoard(size = 8) {
let boardString = "";
for (let rowCount = 1; rowCount <= size; rowCount++) {
let row = "";
for (let count = 1; count <= size; count++) {
if (rowCount % 2) {
row += count % 2 === 0 ? '#' : ' ';
} else {
row += count % 2 === 0 ? ' ' : '#';
}
}
boardString += row;
boardString += "\n"
}
return boardString;
}
console.log(generateBoard());
console.log('---')
console.log(generateBoard(10));
console.log('---')

Exersises Chapter 03

Functions.
What follows are my solutions.

// Minimum.
function minimum(first, second) {
return first < second ? first : second;
}
console.log(minimum(0, 10));
// → 0
console.log(minimum(0, -10));
// → -10
console.log(minimum(1, 2));
// → 1
// Recursion

function isEven(positiveWholeNumber) {
// added this check and trick after researching how to make negative number positive.
// -
if (positiveWholeNumber < 0) {
return isEven(-positiveWholeNumber)
} else if (positiveWholeNumber === 0) {
return true;
} else if (positiveWholeNumber === 1) {
return false
} else {
return isEven(positiveWholeNumber - 2);
}
}

console.log(isEven(10));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ??

So the -positiveWholeNumber is a neat trick to make negative positive.

a = -2
// → -2
-a
// → 2

I was struggling, and still am but it works. :)

Last one is the Bean Counting

// @see: Bean counting
// https://eloquentjavascript.net/03_functions.html#i-3rsiDgC2do

function countBs(string) {
return countChar(string, 'B');
}

console.log(countBs("BOB"));
// → 2
console.log(countBs("Bigbag"));
// → 1
console.log(countBs("VETZAK"));
// → 0
console.log(countChar("kakkerlak", "k"));
// → 4

function countChar(word, character) {
const length = word.length;
let count = 0;
for (let stringPos = 0; stringPos < length; stringPos++) {
// console.log('curPos', stringPos, word[stringPos])
if (word[stringPos] === character) {
count++;
}
}
return count;
}

The bean counting exersize first countBs was oke, then countChar was easy!

Then I taught was done, but forgot to rewrite countBs to use countChar.

function countBs(string) {
return countChar(string, 'B');
}

Et voila! Onto the next chapter.

Coming soon!