Answer: Declaring a variable and assigning the value 'Volvo'
Javascript Code
let carName = "Volvo";
- Answer: Declaring three variables on a single line
Javascript Code
let firstName = "John", lastName = "Doe", age = 35;
- 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;
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
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
Answer: : Executing the function named myFunction
Javascript Code
function myFunction() {
alert("Hello World!");
}
myFunction(); // calling the function
Answer: Creating the Object
Javascript Code
let person = {
name: "John",
age: 50,
};
alert(`${person.name} is ${person.age}`); // output: John is 50
Answer: fixing the button click
html code
<button onclick="clickFunction()">Click me</button>
Javascript Code
function clickFunction() {
alert("you clicked me");
}
- Answer: Alerting the number of items in an array
Javascript Code
const cars = ["Volvo", "Jeep", "Mercedes"];
alert(cars.length); // output: 3
- 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"];
- Answer: Random number between 0 & 1
Javascript Code
const randomNumber = Math.random();
console.log(randomNumber); //output: random number
- Answer: The largest number of 10 and 20
Javascript Code
const largestNumber = Math.max(10, 20);
console.log(largestNumber); // output: 20
- Answer: Square root of 9
Javascript Code
const squareNumber = Math.sqrt(9);
console.log(squareNumber); // output: 3
- Answer: comparison operator to alert true, when x is greater than y.
Javascript Code
x = 10;
y = 5;
alert(x>y) //output: true
- 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
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
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
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
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
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
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
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
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
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"
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"
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
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"]
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
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
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