top of page

JavaScript Exercise - Part I


Today, I've prepared a set of exercises to help you practice fundamental JavaScript concepts, including variables, user input, conditionals, loops, and arrays. Let's get started!



A brief explanation


First, a brief explanation of the mentioned concepts along with code snippets:


1. Variables: Variables in JavaScript are used to store data that can be used in the program later. They are declared using `var`, `let`, or `const`.

   let name = "John";
   const pi = 3.14;


2. User Input: In a browser environment, you can take user input using the `prompt()` function.

  
   let name = prompt("Enter your name:");
  


3. Conditionals: Conditionals are used to make decisions in your code. The `if`, `else if`, and `else` statements are used for conditionals in JavaScript.

   let number = prompt("Enter a number:");
   if (number > 0) {
       console.log("Positive number");
   } else if (number < 0) {
       console.log("Negative number");
   } else {
       console.log("Zero");
   }



4. Loops: Loops are used to repeat a block of code multiple times. The `for` loop is one type of loop in JavaScript.

   for (let i = 1; i <= 5; i++) {
       console.log(i);
   }


5. Arrays (List in Python): An array is a special type of variable that can store multiple values in a single variable.

   let fruits = ["Apple", "Banana", "Cherry"];
   console.log(fruits[0]);  // Prints "Apple"



6. Print: In JavaScript, you can print information to the console using the `console.log()` function.

   console.log("Hello, world!");



Exercise


  1. Simple Calculator Write a script that asks the user for two numbers and an operator (+, -, *, /). Perform the operation and print the result.

  2. Temperature Converter Create a program that converts temperatures from Fahrenheit to Celsius and vice versa. The user should be able to input a temperature and specify the unit.

  3. Multiplication Table Generate a multiplication table up to 10 for any number input by the user. Use a loop to print each line of the table.

  4. Palindrome Checker Write a script that checks if a word entered by the user is a palindrome (a word that reads the same backward as forward, e.g., "radar").

  5. Array Manipulation Accept an array of numbers from the user, then print the highest and lowest numbers, and the average of all numbers.

Answers


  1. Simple Calculator

let num1 = parseInt(prompt("Enter the first number:"));
let num2 = parseInt(prompt("Enter the second number:"));
let operator = prompt("Enter the operator (+, -, *, /):");

let result;
if (operator == '+') {
    result = num1 + num2;
} else if (operator == '-') {
    result = num1 - num2;
} else if (operator == '*') {
    result = num1 * num2;
} else if (operator == '/') {
    result = num1 / num2;
}

console.log(`The result is ${result}`);

2. Temperature Converter


let temp = parseInt(prompt("Enter the temperature:"));
let unit = prompt("Enter the unit (F for Fahrenheit, C for Celsius):");

let convertedTemp;
if (unit.toUpperCase() == 'F') {
    convertedTemp = (temp - 32) * 5/9;
    console.log(`The temperature in Celsius is ${convertedTemp}`);
} else if (unit.toUpperCase() == 'C') {
    convertedTemp = (temp * 9/5) + 32;
    console.log(`The temperature in Fahrenheit is ${convertedTemp}`);
}

3. Multiplication Table

let number = parseInt(prompt("Enter a number:"));

for (let i = 1; i <= 10; i++) {
   let product = number * i;
   console.log(`${number} * ${i} = ${product}`);
}

4. Palindrome Checker

let word = prompt("Enter a word:");
let reversedWord = word.split('').reverse().join('');

if (word == reversedWord) {
    console.log(`${word} is a palindrome`);
} else {
    console.log(`${word} is not a palindrome`);
}


5. Array Manipulation

let numbers = prompt("Enter numbers separated by comma:")
               .split(',')
               .map(Number);

let maxNumber = Math.max(...numbers);
let minNumber = Math.min(...numbers);
let average = numbers.reduce((a, b) => a + b, 0) / numbers.length;

console.log(`Highest Number: ${maxNumber}`);
console.log(`Lowest Number: ${minNumber}`);
console.log(`Average: ${average}`);



Comments


bottom of page