Datayoung DATAYOUNG.COM™

Internet Corporate Solutions
Software Develop
Software Support
Art/Graphics/Music Design,Studio

Start from Programing ,Network Engineering
Novell Netware Products ,System Support till Windows NT
then turn to Open Source and keep maintain Web/Email System for Corporate

11/12/2024

Basic Decision-Making
const moves = [
{ move: "rock", beats: "scissors" },
{ move: "scissors", beats: "paper" },
{ move: "paper", beats: "rock" }
];

function chooseMove(opponentMove) {
for (let move of moves) {
if (move.beats === opponentMove) {
return move.move;
}
}
return "rock"; // Default move
}

console.log(chooseMove("scissors")); // Output: "rock"
console.log(chooseMove("paper")); // Output: "scissors"

11/12/2024

Rule-Based Systems
const rules = [
{ question: "hello", answer: "Hi there!" },
{ question: "how are you", answer: "I'm doing great, thanks for asking!" },
{ question: "bye", answer: "Goodbye!" }
];

function simpleAI(input) {
input = input.toLowerCase();
for (let rule of rules) {
if (input.includes(rule.question)) {
return rule.answer;
}
}
return "I don't understand that.";
}

console.log(simpleAI("hello")); // Output: "Hi there!"
console.log(simpleAI("how are you")); // Output: "I'm doing great, thanks for asking!"

11/12/2024

use a flat array and reference parent-child relationships using an id and parentId system.

OUTPUT:

[
{
id: 1,
name: "Parent 1",
parentId: null,
children: [
{
id: 2,
name: "Child 1",
parentId: 1,
children: [
{ id: 3, name: "Grandchild 1", parentId: 2, children: [] },
{ id: 4, name: "Grandchild 2", parentId: 2, children: [] }
]
},
{
id: 5,
name: "Child 2",
parentId: 1,
children: []
}
]
},
{
id: 6,
name: "Parent 2",
parentId: null,
children: [
{ id: 7, name: "Child 3", parentId: 6, children: [] }
]
}
];

CODE:
function buildTree(flatArray) {
const idToNodeMap = {};
const tree = [];

// Create a map of id to node
flatArray.forEach(item => {
idToNodeMap[item.id] = { ...item, children: [] };
});

// Assign children to their respective parents
flatArray.forEach(item => {
if (item.parentId === null) {
tree.push(idToNodeMap[item.id]);
} else {
idToNodeMap[item.parentId].children.push(idToNodeMap[item.id]);
}
});

return tree;
}

const tree = buildTree(flatHierarchy);
console.log(tree);

const flatHierarchy = [
{ id: 1, name: "Parent 1", parentId: null },
{ id: 2, name: "Child 1", parentId: 1 },
{ id: 3, name: "Grandchild 1", parentId: 2 },
{ id: 4, name: "Grandchild 2", parentId: 2 },
{ id: 5, name: "Child 2", parentId: 1 },
{ id: 6, name: "Parent 2", parentId: null },
{ id: 7, name: "Child 3", parentId: 6 }
];

Traversal Functions for Hierarchical Data
Once you have a hierarchical structure, you can traverse it recursively to search or perform actions.

function findNodeById(tree, id) {
for (const node of tree) {
if (node.id === id) {
return node;
}
if (node.children.length > 0) {
const result = findNodeById(node.children, id);
if (result) {
return result;
}
}
}
return null;
}

const result = findNodeById(tree, 3); // Find node with id 3
console.log(result);

10 septillion years on one of today’s fastest supercomputersWillow’s performance on this benchmark is astonishing: It pe...
11/12/2024

10 septillion years on one of today’s fastest supercomputers

Willow’s performance on this benchmark is astonishing: It performed a computation in under five minutes that would take one of today’s fastest supercomputers 1025 or 10 septillion years. If you want to write it out, it’s 10,000,000,000,000,000,000,000,000 years.

2024/12/10

Our new quantum chip demonstrates error correction and performance that paves the way to a useful, large-scale quantum computer.

ที่อยู่

Bangkok
10400

เว็บไซต์

แจ้งเตือน

รับทราบข่าวสารและโปรโมชั่นของ Datayoungผ่านทางอีเมล์ของคุณ เราจะเก็บข้อมูลของคุณเป็นความลับ คุณสามารถกดยกเลิกการติดตามได้ตลอดเวลา

แนะนำ

แชร์