हमें एक JavaScript फ़ंक्शन लिखना है जो पहले आर्गुमेंट के रूप में एक स्ट्रिंग और दूसरे आर्गुमेंट के रूप में एक नंबर लेता है।
फ़ंक्शन को स्ट्रिंग के प्रत्येक अल्फाबेट को दिए गए नंबर से अल्फाबेट में ऊपर शिफ्ट करके प्रतिस्थापित करना चाहिए।
शिफ्टिंग को अल्फाबेट की शुरुआत या अंत में वापस रैप करना चाहिए, जैसे a को z के बाद आना चाहिए बजाय undefined या किसी शून्य परिणाम के।
उदाहरण
const str = 'this is a str'; const encryptString = (str = '', num = 1) => { const alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); str = str.toLowerCase(); let res = ""; for (let i = 0; i < str.length; i++) { const letter = str[i]; if (alphabet.indexOf(letter) === -1) { res += letter; continue; } let index = alphabet.indexOf(letter) + num % 26; if (index > 25){ index -= 26; }; if (index < 0){ index += 26; }; if(str[i] === str[i].toUpperCase()){ res += alphabet[index].toUpperCase(); }else{ res += alphabet[index]; }; } return res; }; console.log(encryptString(str, 4));आउटपुट
कंसोल में आउटपुट होगा:
xlmw mw e wxv