xxxxxxxxxx
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5)); // Output: 120
xxxxxxxxxx
// Common JavaScript programming questions
// 1. How to check if a variable is an array?
let myArray = [1, 2, 3];
if (Array.isArray(myArray)) {
console.log('The variable is an array.');
} else {
console.log('The variable is not an array.');
}
// 2. How to loop through an array in JavaScript?
let myArray = [1, 2, 3];
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
// 3. How to sort an array in JavaScript?
let myArray = [3, 2, 1];
myArray.sort();
console.log(myArray);
// 4. How to find the length of an object in JavaScript?
let myObject = { a: 1, b: 2, c: 3 };
let objectLength = Object.keys(myObject).length;
console.log(objectLength);
// 5. How to generate a random number in a specific range in JavaScript?
let randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomNumber);
xxxxxxxxxx
const searchEngineQuery = "java script questions";
// Function to search using a search engine API (replace with your chosen search engine API)
function searchUsingAPI(query) {
// Code to interact with the search engine API and fetch results
// Replace this code with your own API integration logic
return fakeAPIResults(query);
}
// Fake API implementation for demonstration purposes
function fakeAPIResults(query) {
console.log(`Searching for: ${query}`);
return [
"stackoverflow.com - JavaScript",
"developer.mozilla.org - JavaScript Guide",
"w3schools.com - JavaScript Tutorial",
"quora.com - JavaScript Questions and Answers"
];
}
// Perform the search and display the results
const searchResults = searchUsingAPI(searchEngineQuery);
console.log("Search Results:");
searchResults.forEach(result => console.log(result));