首页 > 解决方案 > How i can create a new string with two reversed last letters of another string?

问题描述

I tried use conditional (Ternary) Operator but i dont know how to select just the last and second-last letters of string [enter image description here][1]

标签: javascriptstring

解决方案


Without just giving you the answer, if you're ever stuck with a Javascript problem in which you need to manipulate primitives(like string, number, etc.), I'd suggest you look into the methods available. Here is w3schools' list of string methods:

https://www.w3schools.com/jsref/jsref_obj_string.asp

In your case, you might first try the slice method:

https://www.w3schools.com/jsref/jsref_slice_string.asp

Then, you'll need a way to reverse the string. There's no clear string method for this but there are other approaches such as converting the string into an array, reversing the order of that array, then joining the array elements into a string. See here for solutions like that and others:

https://www.freecodecamp.org/news/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb/


推荐阅读