In JavaScript, arithmetic operations allow you to perform basic mathematical calculations using operators. These operations are foundational in programming and are used to manipulate data, perform calculations, and create algorithms. Here’s an overview of the key arithmetic operations:
-
Addition (
+
):- Combines two numbers to get their sum.
-
Subtraction (
-
):- Finds the difference between two numbers by subtracting one from the other.
-
Multiplication (
*
):- Multiplies two numbers to get their product.
-
Division (
/
):- Divides one number by another to get the quotient.
-
Modulus (
%
):- Returns the remainder of the division of one number by another.
-
Increment (
++
):- Increases a number by one.
-
Decrement (
--
):- Decreases a number by one.
-
Order of Operations:
- JavaScript follows the standard order of operations (PEMDAS: Parentheses, Exponents, Multiplication and Division (left-to-right), Addition and Subtraction (left-to-right)).
Here are some questions focused on JavaScript arithmetic operations:
-
Question 1: Write a JavaScript expression to add two numbers,
a
andb
, and store the result in a variablesum
. What will be the value ofsum
ifa
is7
andb
is3
? -
Question 2: Subtract the variable
b
from the variablea
and store the result in a variabledifference
. What will be the value ofdifference
ifa
is15
andb
is8
? -
Question 3: Multiply two numbers,
a
andb
, and store the result in a variableproduct
. What will be the value ofproduct
ifa
is6
andb
is4
? -
Question 4: Divide the variable
a
by the variableb
and store the result in a variablequotient
. What will be the value ofquotient
ifa
is20
andb
is4
? -
Question 5: Write an expression to find the remainder when
a
is divided byb
and store it in a variableremainder
. What will be the value ofremainder
ifa
is10
andb
is3
? -
Question 6: Write a JavaScript expression that first adds
a
andb
, then multiplies the result byc
. What will be the final result ifa
is2
,b
is3
, andc
is4
? -
Question 7: Create a JavaScript expression to calculate the average of three numbers
a
,b
, andc
. What will be the average ifa
is5
,b
is10
, andc
is15
? -
Question 8: Write an expression that increments the value of
a
by1
and then multiplies it byb
. What will be the result ifa
is7
andb
is5
?
JavaScript functions are reusable blocks of code designed to perform a specific task. Functions take input, process it, and return a result. They help in organizing and reusing code efficiently. Here’s an overview of how functions work:
-
Function Declaration:
- A function is declared using the
function
keyword, followed by a name, parameters (optional), and a block of code.
function functionName(parameter1, parameter2) { // Code to execute }
- A function is declared using the
-
Function Invocation:
- Functions are called or invoked using their name followed by parentheses.
functionName(argument1, argument2);
-
Return Statement:
- Functions can return a value using the
return
statement.
function add(a, b) { return a + b; }
- Functions can return a value using the
Here are some questions related to JavaScript functions and arithmetic:
-
Question 9: Write a function
addNumbers
that takes two parametersa
andb
, adds them, and returns the result. Call this function with the values7
and3
. What will be the returned value? -
Question 10: Create a function
subtractNumbers
that takes two parametersa
andb
, subtractsb
froma
, and returns the result. Call this function with the values15
and8
. What will the function return? -
Question 11: Define a function
multiplyNumbers
that multiplies two numbers passed as parameters. Call the function with6
and4
. What will be the returned value? -
Question 12: Write a function
divideNumbers
that takes two parametersa
andb
and returns the quotient ofa
divided byb
. Call this function with the values20
and4
. What is the returned result? -
Question 13: Create a function
calculateRemainder
that accepts two parametersa
andb
, and returns the remainder whena
is divided byb
. What will be the result when you call the function witha = 10
andb = 3
? -
Question 14: Write a function
complexCalculation
that adds two numbersa
andb
, multiplies the result by a third numberc
, and returns the final result. What will be the result of calling the function witha = 2
,b = 3
, andc = 4
? -
Question 15: Define a function
averageOfThree
that takes three parametersa
,b
, andc
, and returns their average. What is the result when the function is called witha = 5
,b = 10
, andc = 15
? -
Question 16: Write a function
incrementAndMultiply
that takes two parametersa
andb
, incrementsa
by 1, multiplies it byb
, and returns the result. What will be the result of calling the function witha = 7
andb = 5
? -
Question 17: Create a function
power
that takes two parametersbase
andexponent
, and returnsbase
raised to the power ofexponent
. What will be the result whenbase = 3
andexponent = 4
? -
Question 18: Write a function
isEven
that takes a single parameternumber
and returnstrue
if the number is even, andfalse
if it is odd. Call this function with the value12
. What will be the returned value?