0

I'm searching for how can I check if word has a same end and start. There is my code:

JavaScript:

function Check() {
  var value = document.getElementById("input").value;

  if (value == startsWith(endsWith()) && value == endsWith(startsWith())) {
    return alert("Yes");

  } else {
    return alert("No");
  }
}
<input id="input" style="margin:20px;" type="text"><br>
<button id="button" onclick="Check();">Check</button>
<p id="demo" style="color:white;">Answer</p>
Saeed
  • 5,413
  • 3
  • 26
  • 40

7 Answers7

2

Just check the first and last character of the string:

if(value[0] == value[value.length-1]) 
{
   alert('Yes');
}
else
{ 
   alert('No');
}
Harsh Jaswal
  • 447
  • 3
  • 14
1

Try below function. I am comparing first character and last character of your input string.

function Check() {
  var value = document.getElementById("input").value;

  if (value.charAt(0) === value.charAt(value.length - 1)) {
    return alert("Yes");

  } else {
    return alert("No");
  }
}
Yogesh Patel
  • 818
  • 2
  • 12
  • 26
1

Use str.length - 1 to get the index of the last character of the string.

function check(str) {
  if (str[0] === str[str.length - 1]) {
    alert('yes')
  } else {
    alert('no');
  }
}

check('test');
check('tests');

To check first/last n characters you can do this.

function checkMore(str, amount) {
  if (str[0] !== str[str.length - 1]) {
    alert('no');
    return;
  } else if (amount !== 1) {
    return checkMore(str.slice(1, str.length - 1), amount - 1);
  }
  alert('yes');
}

checkMore('stests', 1);
checkMore('stests', 2);
checkMore('stests', 3);

And to check the whole string (basically checking if the string is palindrome).

function checkPalindrome(str) {
  if (str.length === 0 || str.length === 1) {
    alert('yes');
    return;
  }
  if (str[0] !== str[str.length - 1]) {
    alert('no');
    return;
  }
  checkPalindrome(str.slice(1, str.length - 1));
}

checkPalindrome('car');
checkPalindrome('carac');
checkPalindrome('carrac');
Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
  • Thanks for your work, I appreciate that, but I already found my answer. –  May 22 '18 at 06:00
1

You can use match() or indexOf(). Both work, but both search the whole string. It is more efficient to extract the substring in the relevant place and compare it with the one you expect there:

function Check ( word ) {
  return word.charAt(0) == word.charAt( word.length - 1 )
}
<button tye="button" onclick="alert( Check('Hello') )">Check 'Hello'</button>
<button tye="button" onclick="alert( Check('HelloH') )">Check 'HelloH'</button>

Of course, you might as well use a regular expression, with a smart regex engine it should be efficient as well (and your code is more concise):

function Check ( word ) {
  return /^(.).*\1$|^.$/.test( word )
}
<button tye="button" onclick="alert( Check('Hello') )">Check 'Hello'</button>
<button tye="button" onclick="alert( Check('WoW') )">Check 'WoW'</button>
Kavian K.
  • 1,340
  • 1
  • 9
  • 11
0

var value = "wow"
return (value[0] === value[value.length - 1] ? true : false)

0th index will contain starting character and value.length - 1 will contain ending character. If they are equal it will return true and false otherwise.

Niragh
  • 162
  • 2
  • 10
0

You can use .split('' ) to split a string by spaces, so into words. It splits it into an array, so you can compare index [0] to index [length-1]

For example

    function Check(string) {
        let arr = string.split(' ');
        return arr[0] == arr[arr.length-1];
    }
Justin
  • 77
  • 1
  • 9
0

Use regex:

if (/^(.).*\1$|^.$/.test(value))
    // first and last are same letter
Bohemian
  • 412,405
  • 93
  • 575
  • 722