new class js -advanced

This commit is contained in:
Philippe Torrel
2026-06-29 11:35:03 +02:00
parent ee8a87bf2a
commit 31c13250b0
104 changed files with 2632 additions and 138 deletions

View File

@@ -0,0 +1,50 @@
'use strict';
const board2string = (board) => board.map((row) => row.join('')).join('\n');
const execMove = (board, move) => {
const originX = fieldToXPosition(originField(move));
const originY = fieldToYPosition(originField(move));
const targetX = fieldToXPosition(targetField(move));
const targetY = fieldToYPosition(targetField(move));
board[targetY][targetX] = board[originY][originX];
board[originY][originX] = emptyBoard()[originY][originX];
return board;
};
const boardInStartPosition = () => [
['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],
['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],
['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'],
];
const emptyBoard = () => [
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
];
const originField = (move) => move.substr(0, 2);
const targetField = (move) => move.substr(2);
const fieldToXPosition = (field) => letterToChessIndex(field.charAt(0));
const fieldToYPosition = (field) => numberToChessIndex(field.charAt(1));
const letterToChessIndex = (letter) => 'abcdefgh'.indexOf(letter);
const numberToChessIndex = (num) => 8 - num;
console.log(board2string(boardInStartPosition()));
console.log('\n');
console.log(board2string(execMove(boardInStartPosition(), 'e2e4')));

View File

@@ -0,0 +1,56 @@
'use strict';
const board2string = (board) => board.map((row) => row.join('')).join('\n');
const execMoves = (moves) => moves.reduce(execMove, boardInStartPosition());
const execMove = (board, move) => {
const originX = fieldToXPosition(originField(move));
const originY = fieldToYPosition(originField(move));
const targetX = fieldToXPosition(targetField(move));
const targetY = fieldToYPosition(targetField(move));
board[targetY][targetX] = board[originY][originX];
board[originY][originX] = emptyBoard()[originY][originX];
return board;
};
const boardInStartPosition = () => [
['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],
['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],
['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'],
];
const emptyBoard = () => [
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
];
const originField = (move) => move.substr(0, 2);
const targetField = (move) => move.substr(2);
const fieldToXPosition = (field) => letterToChessIndex(field.charAt(0));
const fieldToYPosition = (field) => numberToChessIndex(field.charAt(1));
const letterToChessIndex = (letter) => 'abcdefgh'.indexOf(letter);
const numberToChessIndex = (num) => 8 - num;
console.log(board2string(boardInStartPosition()));
console.log('\n');
console.log(board2string(execMove(boardInStartPosition(), 'e2e4')));
console.log('\n');
console.log(board2string(execMoves(['e2e4', 'e7e5', 'f2f4'])));

View File

@@ -0,0 +1,55 @@
'use strict';
const board2string = (board) => board.map((row) => row.join('')).join('\n');
const positionHistoryForMoves = (moves) =>
moves.map((move, i) => moves.slice(0, i + 1)).map(execMoves);
const execMoves = (moves) => moves.reduce(execMove, boardInStartPosition());
const execMove = (board, move) => {
const originX = fieldToXPosition(originField(move));
const originY = fieldToYPosition(originField(move));
const targetX = fieldToXPosition(targetField(move));
const targetY = fieldToYPosition(targetField(move));
board[targetY][targetX] = board[originY][originX];
board[originY][originX] = emptyBoard()[originY][originX];
return board;
};
const boardInStartPosition = () => [
['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],
['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],
['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'],
];
const emptyBoard = () => [
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
['◻', '▦', '◻', '▦', '◻', '▦', '◻', '▦'],
['▦', '◻', '▦', '◻', '▦', '◻', '▦', '◻'],
];
const originField = (move) => move.substr(0, 2);
const targetField = (move) => move.substr(2);
const fieldToXPosition = (field) => letterToChessIndex(field.charAt(0));
const fieldToYPosition = (field) => numberToChessIndex(field.charAt(1));
const letterToChessIndex = (letter) => 'abcdefgh'.indexOf(letter);
const numberToChessIndex = (num) => 8 - num;
const history = positionHistoryForMoves(['e2e4', 'e7e5', 'f2f4']);
console.log(history);
history.forEach((position) => console.log(board2string(position), '\n'));

View File

@@ -0,0 +1,14 @@
const gradesTable = [
['Name', 'Mathematics', 'English', 'Biology'],
['Anna', 85, 92, 78],
['Ben', 90, 88, 84],
['Clara', 76, 95, 89],
];
// Change Anna's math grade to 88
gradesTable[1][1] = 88;
// Add a new row for another student
gradesTable.push(['David', 82, 79, 91]);
console.log(gradesTable);

View File

@@ -0,0 +1,27 @@
'use strict';
const inventory = [
[
// Category: Electronics
['Shelf 1', 'Laptop', 10],
['Shelf 2', 'Smartphone', 25],
],
[
// Category: Clothing
['Shelf 1', 'T-Shirts', 50],
['Shelf 2', 'Jeans', 30],
],
[
// Category: Groceries
['Shelf 1', 'Milk', 100],
['Shelf 2', 'Bread', 80],
],
];
// Increase the number of laptops
inventory[0][0][2] += 5; // New quantity: 15
// Add a new shelf to a category
inventory[1].push(['Shelf 3', 'Pants', 20]);
console.log(inventory);