What is the method for determining the total, product, and remainder when working with a pair of global variables and a pair

I am new to JavaScript and struggling to understand the logic in my code. I have attempted to write some logic below, but I can't quite figure out what code to use to get the desired result. Could someone please assist me in correcting my code and explaining how to achieve the intended outcome?

JS

// Global Variable 
var a = 40;
var b = 70;

function var_ops_5() {
 
  // Local Variables 
  var a = 4;
  var b = 7;
  var c = a + b;
  var d = a * b;
  var e = a % c;
};

Answer №1

The issue lies in the usage of var. It is not advised to declare a variable as var = 40 and then try to declare another variable var a = 4 within the same block. This is because with var, variables declared inside a block can be accessed from outside the block. Consequently, the declaration of var a = 4 inside your function will overwrite the initial var = 40. To resolve this issue, consider using the let keyword instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Applying JavaScript to HTML - Implementing a function to only display an input statement when the "Yes" option is selected, otherwise keeping it hidden

I am currently delving into the world of JavaScript, aiming to comprehend its intricacies. With limited experience in this language, I have managed to successfully hide and unhide radio buttons based on user input. My next challenge lies in achieving a si ...

Anticipating the loading of the next page in JavaScript

Can I use JavaScript to open a new window and then use jQuery to wait until the new page finishes loading? I attempted the following method, but it was unsuccessful: var win = window.open(url,'',windowSpec); $(win.window).load( function () ...

filter supabase to only show items with numbers greater than or equal to a

Hey there! Currently, I am in the process of setting up a store using nextjs pages router and supabase. However, I have encountered a peculiar bug with my product filtering system when dealing with numbers exceeding 4 digits (e.g., 11000). The structure o ...

Cannot get the arrow to rotate using Jquery's css() function

Having trouble using css() to rotate my arrow $(function() { $("#arrowAnim").css("transform:","rotate(300deg)"); }); Check out the fiddle here: https://jsfiddle.net/u4wx093a/10/ I've looked for similar issues but can't seem to solve it on my o ...

What is the best way to develop a widget that loads asynchronously by implementing AJAX, JavaScript, and PHP?

Currently, this widget is in need of items that are sourced from a php file. For instance, the javascript function should generate a table within this div element. <div id="widget"></> The aim is to dynamically update the content with the ht ...

leveraging express.js middleware alongside jwt and express-jwt for secured authentication in express framework

I am encountering an issue while using the express-jwt to create a custom middleware. The error message persists as follows: app.use(expressJwt({ secret: SECRET, algorithms: ['HS256']}).unless({path: ['/login', '/']})); ...

Vue.js experiencing issue with rendering Three.js library

When I began my project with vue init webpack my-project, I found myself focusing on three key files: main.js, AudioPlayer.vue, and App.vue. Despite not encountering any errors, I couldn't figure out why the three.js code was not rendering in the App. ...

The object is identified as 'unknown' within a generic component (error code: 2571)

In my Table component, I have implemented generics and used a static component within the Table to create columns, also with generics. My issue lies in wanting the Column component to inherit the generic type passed to the Table during the execution of th ...

Concealing other items in an array on selecting one item in React Native- A step-by-step guide

Currently, I have set up my view to display an array of items (Circle components) as shown in the image below: However, I am facing a challenge in hiding all other Circle components when I click on one of them. The onPress event is configured to zoom in a ...

In my React application, I have integrated p5 sketches that constantly loop and repeat

Currently facing a challenge integrating p5 sketches into my React App. I've tried adjusting the z Index of the toolbar to place the p5 elements underneath, but they end up repeating instead. Check out the repository here (sketches can be found in the ...

learning how to combine two json arrays of objects and showcase them in a react component

What is the best way to combine this data and present it in a table with a map using React? The text will be in the first column and the count in the second. const handleSubmit = async (event) => { event.preventDefault(); let URL1 = " ...

Material Design Autocomplete search feature in Angular 2

I'm encountering some challenges with autocomplete in Angular2 Material Design. Here are the issues I'm facing: 1. When I type a character that matches what I'm searching for, it doesn't display in the autocomplete dropdown as shown in ...

Ways to 'supply' database records to an interface

The issue at hand is straightforward: retrieving rows from a database and transmitting them to an interface. For example, one possible implementation of this interface would involve writing this data to an XML file. I am in search of a design pattern wher ...

Having trouble fetching the value with the designated ID

I'm encountering an issue with retrieving a value using an id. In my code, I have it set up like this: view <input type="text" value="<?php echo $add->class;?>" name="cls_<?php echo $add->id;?>" id="cls_<?php echo $add->id ...

Error encountered in Typescript when attempting to invoke axios - the call lacks a suitable overload

When I make a call to axios, I include a config object like this: const req = { method, url, timeout: 300000, headers: { 'Content-Type': 'application/json' } } axios(req) An error in TypeScript is thrown stating that "No overload matc ...

Incorporating jsbi into a TypeScript project while adhering to strict mode

I have been developing a TypeScript library that relies on native BigInts. While it functions perfectly in Chrome, I encountered some issues with Safari. After some research, I stumbled upon the jsbi "polyfill" that seems to solve this problem. Unfortunat ...

Tips for inserting a line break in a script

Hello, I need some assistance with creating a typewriter effect using JS, CSS, and HTML. Everything seems to be working fine except when I try to add a new line of text, it doesn't display properly. var str = "<p>I want to put text here then ...

When using the JavaScript .sort() method, any undefined value must always be considered as coming before any other value

I am working on sorting an array of objects based on multiple fields, typically around 3-4 fields. Some values within the objects may be undefined, and I need to ensure that these undefined values are considered as "earlier" in the sorting process, wheth ...

Display the Slug in the Website URL upon clicking on a Blog Post using Angular and Strapi framework

When a user clicks on a blog, I want the URL to display the slug instead of the ID. My frontend framework is Angular and I have created a service to fetch data from the backend built with Strapi. Currently, when selecting a blog from the view, the URL disp ...

Is the ID Column in the Minimal Material Table Demo not appearing as expected?

Hey there, I'm in the process of developing a simple demonstration of a material table - Take note that this is a stackblitz link and for some reason, the id column isn't showing up. Here's a snippet from my app.component.ts: import { C ...