Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HamzaMath #1702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions extensions/HamzaMath
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be:

(function(Scratch) {
  "use strict";

const extension = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your extension needs to be wrapped in a class, and its block functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually any object is fine lol

getInfo: function() {
return {
id: 'hamzaMath',
name: 'HamzaMath',
color: '#006400',
blocks: [
// Separator between the two groups
{
opcode: 'separator1',
blockType: Scratch.BlockType.LABEL,
text: '--- Calculate ---',
group: 'Calculates' // Group for arithmetic operations
},
// Arithmetic operations group
{
opcode: 'calculate',
blockType: Scratch.BlockType.REPORTER,
text: 'Calculate: [EXPRESSION]',
arguments: {
EXPRESSION: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1 + 1'
}
},
group: 'Calculates' // Group for the blocks
},
{
opcode: 'addMultiple',
blockType: Scratch.BlockType.REPORTER,
text: 'Add: [NUMBERS]',
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1, 2, 3'
}
},
group: 'Calculates' // Group for the blocks
},
{
opcode: 'calculateMultiple',
blockType: Scratch.BlockType.REPORTER,
text: 'Multiple: [NUMBERS]',
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1, 2, 3'
}
},
group: 'Calculates' // Group for the blocks
},
{
opcode: 'subtract',
blockType: Scratch.BlockType.REPORTER,
text: 'Subtract: [NUMBERS]',
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1, 2'
}
},
group: 'Calculates' // Group for the blocks
},
{
opcode: 'multiply',
blockType: Scratch.BlockType.REPORTER,
text: 'Multiply: [NUMBERS]',
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1, 2'
}
},
group: 'Calculates' // Group for the blocks
},
{
opcode: 'divide',
blockType: Scratch.BlockType.REPORTER,
text: 'Divide: [NUMBERS]',
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1, 2'
}
},
group: 'Calculates' // Group for the blocks
},
{
opcode: 'modulus',
blockType: Scratch.BlockType.REPORTER,
text: 'Modulus: [NUMBERS]',
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1, 2'
}
},
group: 'Calculates' // Group for the blocks
},
// Separator between the two groups
{
opcode: 'separator2',
blockType: Scratch.BlockType.LABEL,
text: '--- Convert ---',
group: 'Convert' // Conversion group
},
// Conversion group
{
opcode: 'toPositive',
blockType: Scratch.BlockType.REPORTER,
text: 'Convert [NUMBER] to positive',
arguments: {
NUMBER: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1
}
},
group: 'Convert' // Conversion group
},
{
opcode: 'toNegative',
blockType: Scratch.BlockType.REPORTER,
text: 'Convert [NUMBER] to negative',
arguments: {
NUMBER: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1
}
},
group: 'Convert' // Conversion group
}
]
};
},
calculate: function(args) {
try {
const result = eval(args.EXPRESSION);
return result;
} catch (error) {
return 'Error'; // Return error message for invalid expression
}
Comment on lines +137 to +142
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should never be using eval like this. People will be able to run any javascript code, which is dangerous

},
addMultiple: function(args) {
const numbers = args.NUMBERS.split(',').map(Number);
return numbers.reduce((sum, num) => sum + num, 0); // Sum the numbers
},
calculateMultiple: function(args) {
const numbers = args.NUMBERS.split(',').map(Number);
return numbers.reduce((sum, num) => sum + num, 0); // Sum the numbers
},
subtract: function(args) {
const numbers = args.NUMBERS.split(',').map(Number);
return numbers.reduce((diff, num) => diff - num); // Subtract the numbers
},
multiply: function(args) {
const numbers = args.NUMBERS.split(',').map(Number);
return numbers.reduce((product, num) => product * num, 1); // Multiply the numbers
},
divide: function(args) {
const numbers = args.NUMBERS.split(',').map(Number);
if (numbers.includes(0)) return 'Error: Division by zero'; // Check for division by zero
return numbers.reduce((quotient, num) => quotient / num); // Divide the numbers
},
modulus: function(args) {
const numbers = args.NUMBERS.split(',').map(Number);
return numbers.reduce((result, num) => result % num); // Modulus of the numbers
},
toPositive: function(args) {
return Math.abs(args.NUMBER); // Convert the number to positive
},
toNegative: function(args) {
return -Math.abs(args.NUMBER); // Convert the number to negative
}
};

Scratch.extensions.register(extension);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be

  Scratch.extensions.register(new SPAddedMotion());
})(Scratch);

})();