JavaScript에서 Palindrome 검사기를 만드는 방법
14361 단어 htmljavascriptbeginnerswebdev
90%의 사람들이 Palindrome이 무엇인지 알고 있는 것으로 알고 있지만, Palindrome이 무엇인지, 어떤 용도로 사용되는지 모르는 사용자가 많습니다. Palindrome은 실제로 단어, 구절, 문장 또는 앞뒤로 같은 숫자를 읽습니다.
JavaScript의 Palindrome 검사기
이제 이 디자인으로 갑시다. 이것은 디스플레이와 입력 상자가 있는 간단한 프로젝트입니다. 입력란에 무언가를 입력하면 디스플레이에서 결과를 볼 수 있습니다. Watch its live demo 작동 방식을 알아보십시오.
이제 어떻게 만드는지 궁금하실 것입니다. 이 JavaScript Palindrome Checker는 만들기가 매우 쉽지만 이를 위해서는 약간의 JavaScript를 알아야 합니다.
HTML 코드
다음 html은 기본 구조와 정보를 추가합니다.
<!--Basic structure-->
<div class="container">
<!--result box-->
<p id="result"></p>
<!--input box -->
<input type="text" id="input-text" placeholder="Enter a word to check">
<!-- submit button -->
<button id="btn">Check</button>
</div>
CSS 코드
이제 CSS를 디자인해야 합니다. 먼저 웹 페이지는 일부 CSS로 디자인되었습니다.
min-width: 450px 및 배경색: #ffffff를 사용합니다. padding: 13px 7px 에 따라 다릅니다. /*Basic design of webpage*/
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
background-color: #1f85e0;
}
/*Basic structure of Palindrome Checker*/
.container{
width: 35%;
min-width: 450px;
background-color: #ffffff;
padding: 50px 35px;
position: absolute;
transform: translate(-50%,-50%);
left: 50%;
top: 50%;
text-align: center;
border-radius: 8px;
box-shadow: 0 20px 25px rgba(0,0,0,0.18);
}
.container *{
font-family: "DM Sans", sans-serif;
outline: none;
}
/*Design the input box*/
input{
width: 100%;
border: none;
border: 2px solid #d5d5d5;
padding: 13px 7px;
font-weight: 400;
}
input:focus{
border-bottom: 2px solid #b156fe;
}
/*Design of submit button*/
button{
width: 130px;
padding: 11px;
background-color: #185bad;
border: none;
border-radius: 4px;
color: #ffffff;
font-weight: 400;
margin-top: 30px;
font-size: 17px;
}
/*Design the result area*/
p{
text-align: center;
color: #073b8c;
font-weight: 500;
padding: 30px;
margin-bottom: 40px;
font-size: 22px;
box-shadow: 0 0 20px rgba(0,139,253,0.25);
}
자바스크립트 코드
Palindrome Checker JavaScript 디자인 작업은 끝났지만 실제 작업은 아직 남아있습니다.
활성화하려면 JavaScript를 사용해야 합니다. 하지만 초보자도 걱정할 필요가 없습니다. 나는 필요한 모든 설명을 했다.
'txt'에 저장됩니다. 'textContent' 결과를 디스플레이에 표시하도록 배치했습니다.document.getElementById("btn").addEventListener("click",function(){
//Input value has been collected
let txt = document.getElementById("input-text").value;
checkPalindrome(txt);
});
//I have given all the calculations below
function checkPalindrome(txt){
//'a-zA-Z0-9' will match all alphanumeric characters, i.e. lower case alphabet, upper case alphabet, and all numbers
//The "g" modifier specifies a global match.
let txt_new = txt.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
let len = txt_new.length;
//'len' Indicates the total number of characters present in it
let halfLen = Math.floor( len/2 );
let result =document.getElementById("result");
let i;
for( i = 0; i < halfLen; i++){
// !== is strict inequality operator
//strict inequality operator checks whether its two operands are not equal, returning a Boolean result.
if( txt_new[i] !== txt_new[len-1-i]){
result.textContent = "Sorry! Not a palindrome 😔";
return;
}
//textContent property sets or returns the text content of the specified node
result.textContent = "Yes! It's a palindrome 😍"
}
}
위의 JavaScript 줄을 이해하는 데 문제가 없기를 바랍니다.
Please comment html에서 이 palindrome 프로그램이 마음에 드는지. 위에서 이 JavaScript Palindrome Checkerlive preview link를 공유했습니다.
Reference
이 문제에 관하여(JavaScript에서 Palindrome 검사기를 만드는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/shantanu_jana/how-to-create-a-palindrome-checker-in-javascript-4pe7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)