हमें एक JavaScript फ़ंक्शन लिखना है जो पहली आर्गुमेंट के रूप में एक संख्या n और दूसरी आर्गुमेंट के रूप में संख्याओं की एक सरणी लेता है। फ़ंक्शन को सबसे छोटी n अंकों की संख्या लौटानी चाहिए जो सरणी में निर्दिष्ट सभी तत्वों का गुणक हो। यदि ऐसी कोई n अंकों की संख्या मौजूद नहीं है तो हमें सबसे छोटा ऐसा तत्व लौटाना चाहिए।
उदाहरण
यदि सरणी है:
const arr = [12, 4, 5, 10, 9]
n = 2 और n = 3 दोनों के लिए, आउटपुट 180 होना चाहिए।
कोड
const arr = [12, 4, 5, 10, 9];
const num1 = 2; const num2 = 3;
const allDivides = (arr, num) => arr.every(el => num % el === 0);
const smallestMultiple = (arr, num) => {
let smallestN = Math.pow(10, (num - 1));
while (!allDivides(arr, smallestN)) { smallestN++; }
return smallestN;
};
console.log(smallestMultiple(arr, num1));
console.log(smallestMultiple(arr, num2));
आउटपुट
180 180