Skip to content

navya-javascript-operators-assignment#542

Open
navya-1417 wants to merge 1 commit intoaptyInc:mainfrom
navya-1417:navya-operators-assignments
Open

navya-javascript-operators-assignment#542
navya-1417 wants to merge 1 commit intoaptyInc:mainfrom
navya-1417:navya-operators-assignments

Conversation

@navya-1417
Copy link
Copy Markdown

@navya-1417 navya-1417 commented Nov 10, 2025

Terms and Conditions

  • I Accept losing points if my PR does not follow the best practices mentioned below, which will impact my overall performance in training

HTML Best Practices

  • File Naming Convention:

  • Follow consistent and descriptive naming (e.g., dashboard.html, user-profile.html).

  • Use lowercase letters and hyphens instead of spaces.

  • Page Title:

  • Ensure the <title> tag is descriptive and aligns with the page content.

  • Include meaningful keywords for SEO if applicable.

  • Semantic Markup:

  • Use appropriate tags like <header>, <footer>, <section>, <article> for better readability and accessibility.

  • Accessibility Standards:

  • Ensure the use of alt attributes for images and proper labels for form elements.

  • Use ARIA roles where necessary.

  • Validation:

  • Ensure the code passes HTML validation tools without errors or warnings.

  • Structure and Indentation:

  • Maintain consistent indentation and proper nesting of tags.

  • Attributes:

  • Ensure all required attributes (e.g., src, href, type, etc.) are correctly used and not left empty.

CSS Best Practices

  • File Organization:

  • Use modular CSS files if applicable (e.g., base.css, layout.css, theme.css).

  • Avoid inline styles unless absolutely necessary.

  • Naming Conventions:

  • Use meaningful class names following BEM or other conventions (e.g., block__element--modifier).

  • Code Reusability:

  • Avoid duplicate code; use classes or mixins for shared styles.

  • Responsive Design:

  • Ensure proper usage of media queries for mobile, tablet, and desktop views.

  • Performance Optimization:

  • Minimize the use of unnecessary CSS selectors.

  • Avoid overly specific selectors and ensure selectors are not overly deep (e.g., avoid #id .class1 .class2 p).

  • Consistency:

  • Follow consistent spacing, indentation, and use of units (rem/em vs. px).

  • Maintain a single coding style (e.g., always use double or single quotes consistently).

Javascript Best Practices

  • File Organization:

  • Ensure scripts are modular and logically separated into files if needed.

  • Avoid mixing inline JavaScript with HTML.

  • Logic Optimization:

  • Check for redundancy and ensure the code is optimized for performance.

  • Avoid unnecessary API calls or DOM manipulations.

  • Solution Approach:

  • Confirm that the code solves the given problem efficiently.

  • Consider scalability for future enhancements.

  • Readability:

  • Use clear variable and function names.

  • Add comments for complex logic or algorithms.

  • Error Handling:

  • Ensure proper error handling for API calls or user input validation.

  • Code Quality:

  • Check for potential bugs (e.g., missing await, mishandling of null/undefined values).

  • Avoid unnecessary console.log statements in production code.

  • Security:

  • Avoid hardcoding sensitive data.

  • Sanitize user input to prevent XSS and other vulnerabilities.

  • Best Practices:

  • Use const and let instead of var.

  • Follow ES6+ standards where applicable.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @navya-1417, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request adds a comprehensive collection of interactive web applications designed to illustrate the functionality of various JavaScript operators. Each application is self-contained with its own HTML and JavaScript files, providing clear examples for arithmetic, assignment, comparison, logical, logical assignment, ternary, and bitwise operations. The changes aim to enhance the learning resources by offering practical, hands-on demonstrations of fundamental JavaScript concepts, all accessible from a single overview page.

Highlights

  • New JavaScript Operator Assignments: This pull request introduces seven new assignments focused on demonstrating various JavaScript operators, including arithmetic, assignment, comparison, logical, logical assignment, ternary, and bitwise operators.
  • Interactive Demos: Each assignment features an interactive HTML page with corresponding JavaScript logic, allowing users to input values and observe the results of different operator operations.
  • Centralized Access: A main index.html file has been added to provide a consolidated entry point, embedding all seven assignments within collapsible sections for easy navigation and demonstration.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @navya-1417, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request delivers a comprehensive set of interactive web-based assignments designed to educate users on the diverse range of JavaScript operators. By providing practical examples for arithmetic, assignment, comparison, logical, logical assignment, ternary, and bitwise operations, it aims to enhance understanding through hands-on experimentation. The structure allows for easy exploration of each operator category, making it a valuable resource for learning fundamental JavaScript concepts.

Highlights

  • New Operator Assignments: Introduced seven new assignments covering various JavaScript operators: arithmetic, assignment, comparison, logical, logical assignment, ternary, and bitwise operators.
  • Interactive Demos: Each assignment includes an interactive HTML page and corresponding JavaScript logic to demonstrate the functionality of specific operator types.
  • Centralized Access: A main index.html file has been added to provide a consolidated entry point, allowing users to easily navigate and view all operator assignments within iframes.
  • Type Handling in Comparisons: The comparison and logical operator assignments include functionality to cast input values to different types (string, number, boolean, etc.) to illustrate type coercion and strict comparison behaviors.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds several small assignments to demonstrate JavaScript operators. The code is functional for the most part, but there are several opportunities for improvement regarding best practices, performance, and correctness. I've identified issues such as the use of obsolete HTML attributes, inline styles and event handlers which go against modern web development practices, inefficient DOM manipulation by repeatedly querying for the same elements, and a few logic bugs in the operator implementations. My review comments provide specific suggestions to address these points and improve the overall quality of the code.

Comment on lines +45 to +49
function increment() {
let [a] = getValue();
a++;
output.innerHTML = `After Increment: ${a}`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The increment function reads the value from the input, but it doesn't update the input field with the new value. This means that if you click the button multiple times, it will always show the same result. The input's value should be updated to allow for sequential increments.

Suggested change
function increment() {
let [a] = getValue();
a++;
output.innerHTML = `After Increment: ${a}`;
}
function increment() {
let [a] = getValue();
a++;
value1.value = a;
output.innerHTML = `After Increment: ${a}`;
}

Comment on lines +10 to +14
const response = age >= 18
? `Hello ${fullName}, welcome to Apty!`
: `Hello ${fullName}, come back after ${18 - age} years`;

messageBox.textContent = response;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

There is no input validation for the age. If a user enters a non-numeric value, age will be NaN, and the output message will be "... come back after NaN years". You should add a check to handle invalid age inputs.

    const response = Number.isNaN(age)
        ? "Please enter a valid age."
        : age >= 18
            ? `Hello ${fullName}, welcome to Apty!`
            : `Hello ${fullName}, come back after ${18 - age} years`;

    messageBox.textContent = response;

Comment on lines +40 to +44
function increase() {
let [a] = fetchValues();
a++;
resultBox.innerHTML = `Increment Result = ${a}`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The increase function doesn't update the value in the input field after incrementing it. This causes the function to produce the same result every time it's clicked, instead of sequentially incrementing the value. To fix this, you should update the input field's value.

Suggested change
function increase() {
let [a] = fetchValues();
a++;
resultBox.innerHTML = `Increment Result = ${a}`;
}
function increase() {
let [a] = fetchValues();
a++;
firstNum.value = a;
resultBox.innerHTML = `Increment Result = ${a}`;
}

Comment on lines +46 to +50
function decrease() {
let [a] = fetchValues();
a--;
resultBox.innerHTML = `Decrement Result = ${a}`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Similar to the increase function, the decrease function does not update the input field's value after the operation. This leads to incorrect behavior on subsequent clicks. The value in the input field should be updated to reflect the change.

Suggested change
function decrease() {
let [a] = fetchValues();
a--;
resultBox.innerHTML = `Decrement Result = ${a}`;
}
function decrease() {
let [a] = fetchValues();
a--;
firstNum.value = a;
resultBox.innerHTML = `Decrement Result = ${a}`;
}

Comment on lines +51 to +55
function decrement() {
let [a] = getValue();
a--;
output.innerHTML = `After Decrement: ${a}`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Similar to the increment function, the decrement function doesn't update the input field's value after the operation. This should be fixed to ensure the UI behaves as expected on multiple clicks.

Suggested change
function decrement() {
let [a] = getValue();
a--;
output.innerHTML = `After Decrement: ${a}`;
}
function decrement() {
let [a] = getValue();
a--;
value1.value = a;
output.innerHTML = `After Decrement: ${a}`;
}

Comment on lines +10 to +14
let first = document.getElementById("valA").value;
let firstType = document.getElementById("typeA").value;

let second = document.getElementById("valB").value;
let secondType = document.getElementById("typeB").value;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The getValues function queries the DOM for elements every time it's called. For better performance, you should get the element references once at the top of the script and reuse those constants within this function.

Comment on lines +4 to +6
const fname = document.getElementById("firstName").value.trim();
const lname = document.getElementById("lastName").value.trim();
const age = Number(document.getElementById("ageValue").value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

These DOM elements are being queried on every click of the 'Check' button. It's more efficient to query them once when the script loads and store them in constants to be reused, avoiding repeated DOM lookups.

<div id="buttons" style="margin-top: 15px;">
<button onclick="add()">+=</button>
<button onclick="subtract()">-=</button>
<button onclick="multiply()">*</button>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The text for this button is *, but it triggers a multiplication assignment operation. For consistency with the other assignment operator buttons (+=, -=), the text should be *=, which better represents the operation being performed.

Suggested change
<button onclick="multiply()">*</button>
<button onclick="multiply()">*=</button>

<input type="number" id="val2" placeholder="Enter Second Value">
</div>

<div id="buttons" style="margin-top: 15px;">
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This div uses an inline style attribute. According to the CSS Best Practices in your pull request description, inline styles should be avoided. It's better to use a separate CSS file or a <style> block to keep your styling separate from your HTML structure. This also applies to the div on line 27.

<body>
<details>
<summary>Assignment 1</summary>
<iframe src="./assignment-1/index.html" frameborder="0" width="100%"></iframe>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The frameborder attribute is obsolete in HTML5 and should not be used. For styling, you should use CSS. You can replace frameborder="0" with style="border: none;". This applies to all <iframe> elements in this file.

Suggested change
<iframe src="./assignment-1/index.html" frameborder="0" width="100%"></iframe>
<iframe src="./assignment-1/index.html" style="border: none;" width="100%"></iframe>

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds seven assignments demonstrating various JavaScript operators. The code is functional and covers the requirements for each assignment. However, there are several areas for improvement across all assignments, mainly related to JavaScript and HTML best practices. Key issues include mixing HTML and JavaScript logic by using onclick attributes, using <br> tags for styling instead of CSS, repetitive code in JavaScript functions that could be refactored for better maintainability, and inefficient DOM access in several scripts. My detailed comments provide specific suggestions to address these points and improve the overall code quality.

Comment on lines +13 to +17
try {
return JSON.parse(value);
} catch {
return { value };
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

When JSON.parse(value) fails, the catch block returns a new object { value }. This can be misleading for the user, as the comparison will likely not behave as expected. It's better to provide feedback to the user that their JSON input is invalid, for example by using alert() or displaying an error message.

Suggested change
try {
return JSON.parse(value);
} catch {
return { value };
}
try {
return JSON.parse(value);
} catch {
alert('Invalid JSON format for object. Please check your input.');
return null; // Or handle the error appropriately
}

<input type="number" id="numB" />
</div>

<br />
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using <br /> tags for visual spacing is discouraged. CSS properties like margin or padding should be used for layout and spacing. This separates the document's structure (HTML) from its presentation (CSS), leading to cleaner and more maintainable code.

Comment on lines +22 to +28
<button onclick="doAdd()">+</button>
<button onclick="doSubtract()">-</button>
<button onclick="doMultiply()">*</button>
<button onclick="doDivide()">/</button>
<button onclick="doModulo()">%</button>
<button onclick="increase()">++</button>
<button onclick="decrease()">--</button>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using onclick attributes in HTML mixes your presentation logic with your application logic. A better practice is to use addEventListener in your JavaScript file to attach event handlers to your DOM elements. This improves separation of concerns, making your code cleaner and easier to maintain.

For example, in index.js:

// Get buttons
document.querySelector('#calcActions button:nth-child(1)').addEventListener('click', doAdd);
document.querySelector('#calcActions button:nth-child(2)').addEventListener('click', doSubtract);
// and so on for all buttons...

This would require removing the onclick attributes from the HTML buttons.

<input type="number" id="val2" placeholder="Enter Second Value">
</div>

<div id="buttons" style="margin-top: 15px;">
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Avoid using inline styles like style="margin-top: 15px;". This mixes presentation with structure, making the code harder to maintain. It's better to move styling to a separate CSS file or a <style> block in the <head> section. This improves separation of concerns and allows for easier management of styles across your application.

Suggested change
<div id="buttons" style="margin-top: 15px;">
<div id="buttons">

<div id="buttons" style="margin-top: 15px;">
<button onclick="add()">+=</button>
<button onclick="subtract()">-=</button>
<button onclick="multiply()">*</button>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There's a small typo on this button. To match the purpose of demonstrating assignment operators, the text should be *= instead of *.

Suggested change
<button onclick="multiply()">*</button>
<button onclick="multiply()">*=</button>

if (type === "number") {
return Number(value);
} else if (type === "boolean") {
return value === "true";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This boolean conversion is case-sensitive and will only work for the exact string 'true'. In other assignments (like assignment-4), you use value.toLowerCase() === 'true', which is more robust as it handles 'True', 'TRUE', etc. It's good practice to be consistent and use the more robust version.

Suggested change
return value === "true";
return value.toLowerCase() === "true";

Comment on lines +4 to +6
const fname = document.getElementById("firstName").value.trim();
const lname = document.getElementById("lastName").value.trim();
const age = Number(document.getElementById("ageValue").value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Calling document.getElementById inside a function that runs on every event (like a button click) is inefficient. The browser has to search the entire DOM tree each time. It's a best practice to cache these element references in constants at the top of your script, outside the function. This way, the DOM is only queried once when the script loads.

Consider defining your element constants at the top of the file:

const firstNameInput = document.getElementById("firstName");
const lastNameInput = document.getElementById("lastName");
const ageValueInput = document.getElementById("ageValue");

And then reference them in your function:

Suggested change
const fname = document.getElementById("firstName").value.trim();
const lname = document.getElementById("lastName").value.trim();
const age = Number(document.getElementById("ageValue").value);
const fname = firstNameInput.value.trim();
const lname = lastNameInput.value.trim();
const age = Number(ageValueInput.value);

Comment on lines +21 to +73
function andOp() {
try {
const [a, b] = readNums();
show(`${a} & ${b} = ${a & b}`);
} catch (e) {
show(e.message);
}
}

function orOp() {
try {
const [a, b] = readNums();
show(`${a} | ${b} = ${a | b}`);
} catch (e) {
show(e.message);
}
}

function xorOp() {
try {
const [a, b] = readNums();
show(`${a} ^ ${b} = ${a ^ b}`);
} catch (e) {
show(e.message);
}
}

function notOp() {
try {
const [a, b] = readNums();
show(`~${a} = ${~a}, ~${b} = ${~b}`);
} catch (e) {
show(e.message);
}
}

function lshiftOp() {
try {
const [a, b] = readNums();
show(`${a} << ${b} = ${a << b}`);
} catch (e) {
show(e.message);
}
}

function rshiftOp() {
try {
const [a, b] = readNums();
show(`${a} >> ${b} = ${a >> b}`);
} catch (e) {
show(e.message);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There is significant code duplication across all the operator functions (andOp, orOp, etc.). Each function contains an identical try...catch block. This can be refactored into a single, generic function that takes the operator as an argument. This will reduce code, improve readability, and make maintenance easier.

For example, you could have a single handler:

function performBitwiseOperation(operator) {
    try {
        const [a, b] = readNums();
        let result;
        switch (operator) {
            case '&': result = a & b; break;
            case '|': result = a | b; break;
            // ... other cases
        }
        show(`${a} ${operator} ${b} = ${result}`);
    } catch (e) {
        show(e.message);
    }
}

And then call it from your HTML: <button onclick="performBitwiseOperation('&')">&amp; (AND)</button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant