11. Declare an array of five colors and print the first and last colors.
Required Input:
["red", "blue", "green", "yellow", "purple"]
Expected Output:
red
purple
Code In Javascript
// Declare an array and print the first and last elements
let colors = ["red", "blue", "green", "yellow", "purple"];
console.log(colors);
console.log(colors);
Run Code?
Click Run Button to view compiled output
12. Create a function that reverses a string.
Required Input:
"hello"
Expected Output:
olleh
Code In Javascript
// Function to reverse a string
function reverseString(str) {
return;
}
console.log(reverseString("hello"));
Run Code?
Click Run Button to view compiled output
13. Write a program to sum all elements in an array.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
15
Code In Javascript
// Sum all elements in an array
let numbers = [1, 2, 3, 4, 5];
let sum = numbers;
console.log(sum);
Run Code?
Click Run Button to view compiled output
14. Create an object student with properties name, age, and grade. Access and print each property.
Required Input:
{name: "John", age: 20, grade: "A"}
Expected Output:
John
20
A
Code In Javascript
// Declare an object and access its properties
let student = {
name: "John",
age: 20,
grade: "A"
};
console.log();
console.log();
console.log();
Run Code?
Click Run Button to view compiled output
15. Write a function to calculate the factorial of a number.
Required Input:
num = 5
Expected Output:
120
Code In Javascript
// Calculate factorial using a loop
function factorial(num) {
return result;
}
console.log(factorial(5));
Run Code?
Click Run Button to view compiled output
16. Write a program to check if a number is prime.
Required Input:
num = 7
Expected Output:
true
Code In Javascript
// Check if a number is prime
function isPrime(num) {
return true;
}
console.log(isPrime(7));
Run Code?
Click Run Button to view compiled output
17. Create a function to count the number of vowels in a given string.
Required Input:
"hello"
Expected Output:
2
Code In Javascript
// Count the number of vowels in a string
function countVowels(str) {
return count;
}
console.log(countVowels("hello"));
Run Code?
Click Run Button to view compiled output
18. Write a program to remove duplicates from an array.
Required Input:
[1, 2, 2, 3, 4, 4, 5]
Expected Output:
[ 1, 2, 3, 4, 5 ]
Code In Javascript
// Remove duplicates from an array
let numbers = [1, 2, 2, 3, 4, 4, 5];
let uniqueNumbers = ;
console.log(uniqueNumbers);
Run Code?
Click Run Button to view compiled output
19. Create an array of numbers and sort them in ascending order.
Required Input:
[5, 2, 9, 1, 5, 6]
Expected Output:
[ 1, 2, 5, 5, 6, 9 ]
Code In Javascript
// Sort an array in ascending order
let numbers = [5, 2, 9, 1, 5, 6];
let sortedNumbers = ;
console.log(sortedNumbers);
Run Code?
Click Run Button to view compiled output
20. Write a program to find the index of a given element in an array.
Required Input:
array = [10, 20, 30, 40, 50], element = 30
Expected Output:
2
Code In Javascript
// Find index of a given element in an array
let array = [10, 20, 30, 40, 50];
let element = ;
let index = ;
console.log(index);
Run Code?
Click Run Button to view compiled output