Skip to content

EyachirArafat/JavaScript-Assignments-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation


1. JavaScript Basic Problems Solvings

01.

Answer: Declaring a variable and assigning the value 'Volvo'

Javascript Code

let carName = "Volvo";

02.

  1. Answer: Declaring three variables on a single line

Javascript Code

let firstName = "John", lastName = "Doe", age = 35;

  1. Answer:
    1st Variable Name: firstName,
    2nd Variable Values: "Doe"
    3rd Variable Name: age & Values: 35

Javascript Code

let firstName = "John";
let lastName = "Doe";
let age = 35;

03.

Answer: The value of 'x' after using the assignment operator

Javascript Code

let x = 10;
let y = 5;
x *= y; 
console.log(x); // output: 50

04.

Answer: Writting data types by using comments

Javascript Code

let length = 16; // data type: Number
let lastName = "Johnson"; // data type: String

const x = {
  firstName: "John",
  lastName: "Doe",
}; // data type: Object

05.

Answer: : Executing the function named myFunction

Javascript Code

function myFunction() {
  alert("Hello World!");
}
myFunction(); // calling the function

06.

Answer: Creating the Object

Javascript Code

let person = {
  name: "John",
  age: 50,
};
alert(`${person.name} is ${person.age}`); // output: John is 50

07.

Answer: fixing the button click

html code

<button onclick="clickFunction()">Click me</button>

Javascript Code

function clickFunction() {
    alert("you clicked me");
}

08.

  1. Answer: Alerting the number of items in an array

Javascript Code

const cars = ["Volvo", "Jeep", "Mercedes"];
alert(cars.length); // output: 3

  1. Answer: Changing the first item of Brand to "Ford".

Javascript Code

const Brand = ["Volvo", "Jeep", "Mercedes"];
Brand[0] = "Ford";
console.log(Brand); //output: ["Ford", "Jeep", "Mercedes"];

09.

  1. Answer: Random number between 0 & 1

Javascript Code

const randomNumber = Math.random();
console.log(randomNumber); //output: random number

  1. Answer: The largest number of 10 and 20

Javascript Code

const largestNumber = Math.max(10, 20);
console.log(largestNumber); // output: 20

  1. Answer: Square root of 9

Javascript Code

const squareNumber = Math.sqrt(9);
console.log(squareNumber); // output: 3

10.

  1. Answer: comparison operator to alert true, when x is greater than y.

Javascript Code

x = 10;
y = 5;
alert(x>y) //output: true

  1. Answer: Conditional (ternary) operator to alert "Too young" if age is less than 18, otherwise alert "Old enough"

Javascript Code

let age = 15;
alert(age < 18 ? "Too young" : "Old enough"); //output: Too young


2. JavaScript Basic Problems Solving

01.

Answer: A function to convert Celsius to Fahrenheit

Javascript Code

function celsiusToFahrenheit(celsius) {
  return (celsius*9)/5+32;
}
let celsius = 20;
let fahrenheit = celsiusToFahrenheit(celsius);

console.log(fahrenheit); // output: 68

02.

Answer: A function to check if a number is even

Javascript Code

function isEven(num) {
  return num % 2 === 0;
}

console.log(isEven(4)); // output: true
console.log(isEven(7)); // output: false

03.

Answer: A function to sum two numbers

Javascript Code

function sum(a, b) {
  return a+b;
}

console.log(sum(3, 4)); // output: 7
console.log(sum(10, 20)); // output: 30

04.

Answer: A function to find the smallest number in an array

Javascript Code

function findSmallestNum(arr) {
  return Math.min(...arr);
}

console.log(findSmallestNum([3, 5, 1, 9])); // output: 1
console.log(findSmallestNum([-1, -5, 0, 10])); // output: -5

05.

Answer: A function to count the number of vowels in a string

Javascript Code

function countVowels(str) {
  let vowels = 'aeiouAEIOU';
  return str.split('').filter(char => vowels.includes(char)).length;
}

console.log(countVowels("hello world")); // output: 3
console.log(countVowels("Javascript")); // output: 3

06.

Answer: A function to get the first element of an array

Javascript Code

function getFirstElement(arr) {
  return arr[0];
}

console.log(getFirstElement([1, 2, 3])); // output: 1
console.log(getFirstElement(["a", "b", "c"])); // output: a

07.

Answer: A function to check if an array is empty

Javascript Code

function isArrayEmpty(arr){
  return arr.length === 0;
}

console.log(isArrayEmpty([])); // output: true
console.log(isArrayEmpty([1, 2, 3])); // output: false

08.

Answer: A function to return the factorial of a number

Javascript Code

function factorialize(num){
  if (num === 1 || num === 0) return 1;
  return num * factorialize(num - 1);
}

console.log(factorialize(5)); // output: 120
console.log(factorialize(7)); // output: 5040

09.

Answer: A function to reverse a string

Javascript Code

function reverseString(str) {
  return str.split("").reverse().join("");
}

console.log(reverseString("hello")); // output: "olleh"
console.log(reverseString("world")); // output: "dlrow"

10.

Answer: A function to convert a string to lowercase

Javascript Code

function toLowerCase(str) {
  return str.toLowerCase();
}

console.log(toLowerCase("HELLO WORLD")); // output: "hello world"
console.log(toLowerCase("JavaScript")); // output: "javascript"

11.

Answer: A function to find the length of a string

Javascript Code

function stringLength(str) {
  return str.length;
}

console.log(stringLength("hello")); // output: 5
console.log(stringLength("world")); // output: 5

12.

Answer: A function to merge two arrays

Javascript Code

function mergeArrays(arr1, arr2) {
  return [...arr1, ...arr2];
}

console.log(mergeArrays([1, 2, 3], [4, 5, 6])); // output: [1, 2, 3, 4, 5, 6]
console.log(mergeArrays(["a", "b"], ["c", "d"])); // output: ["a", "b", "c", "d"]

13.

Answer: A function to get the last element of an array

Javascript Code

function getLastElement(arr) {
  return arr[arr.length - 1];
}

console.log(getLastElement([1, 2, 3])); // output: 3
console.log(getLastElement(["a", "b", "c"])); // output: c

14.

Answer: A function to get the first character of a string

Javascript Code

function getFirstCharacter(str) {
  return str[0];
}

console.log(getFirstCharacter("hello")); // output: h
console.log(getFirstCharacter("world")); // output: w

15.

Answer:

Javascript Code

function sumArray(arr) {
  return arr.reduce((total, num) => total + num, 0);
}

console.log(sumArray([1, 2, 3, 4])); // output: 10
console.log(sumArray([-1, -2, -3, -4])); // output: -10
console.log(sumArray([1.5, 2.5, 3.5])); // output: 7.5

Thank you © Eyachir

About

JavaScript First Assignments: Basic Problems Solving

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published