0% found this document useful (0 votes)
2 views

Assignment 1

The document contains a series of JavaScript code snippets demonstrating basic programming concepts. It includes creating and manipulating variables, performing arithmetic operations, checking character cases, and applying discounts. Each code example is accompanied by comments explaining the functionality.

Uploaded by

xanjv.exotrac
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 1

The document contains a series of JavaScript code snippets demonstrating basic programming concepts. It includes creating and manipulating variables, performing arithmetic operations, checking character cases, and applying discounts. Each code example is accompanied by comments explaining the functionality.

Uploaded by

xanjv.exotrac
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Create two variables, one for your name and another for your age.

Print a sentence
combining those variables

const name = "Sanjeev Shrestha";


const age = 35;

console.log(`My name is ${name} and I am ${age} years old.`);

2. Assign the value 10 to a variable. Then multiply that variable by itself and store the result
in another variable. Print the final value.

let num = 10;


let squared = num * num;

console.log(squared);

3. Write code to check if the character stored in a variable is uppercase or lowercase. Print
"uppercase" or "lowercase" accordingly. (Hint: Use character codes)

const char = 'A'; // Change this to the character you want to check

// Get the character code


const charCode = char.charCodeAt(0);

// Check if the character is uppercase or lowercase


if (charCode >= 65 && charCode <= 90) {
console.log('uppercase');
} else if (charCode >= 97 && charCode <= 122) {
console.log('lowercase');
} else {
console.log('Not an alphabetic character');
}

4. Declare two variables with numeric values. Add them together and print the sum. Then,
subtract the smaller number from the larger number and store the difference in a new
variable. Print the difference.

const num1 = 25;


const num2 = 15;

const sum = num1 + num2;


console.log(`The sum of ${num1} and ${num2} is: ${sum}`);

const larger = Math.max(num1, num2);


const smaller = Math.min(num1, num2);
const difference = larger - smaller;

console.log(`The difference between ${larger} and ${smaller} is: ${difference}`);


5. Imagine you have a variable storing a product price. Write code to apply a 10% discount
and print the discounted price.

const productPrice = 100; // Original product price


const discountPercentage = 10; // Discount percentage

// Calculate the discount amount


const discountAmount = productPrice * (discountPercentage / 100);

// Calculate the discounted price


const discountedPrice = productPrice - discountAmount;

console.log(`Original price: $${productPrice.toFixed(2)}`);


console.log(`Discount percentage: ${discountPercentage}%`);
console.log(`Discount amount: $${discountAmount.toFixed(2)}`);
console.log(`Discounted price: $${discountedPrice.toFixed(2)}`);

You might also like