如果字符串是“ hello world”, 我需要翻转每对字符并返回“ ehll oowlrd”。 我这样做的方式返回“ olleh dlrow”。
var flipPairs = function (string) {
return string.split("").reverse().join("");
}
console.log(flipPairs(“ hello world”)); //-> ehll oowlrd
答案 0 :(得分:2)
我将使用正则表达式-匹配两个连续的字符,并以相反的顺序替换它们:
writeValueAsString()
答案 1 :(得分:1)
那呢:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="site-wrapper">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Page 1-1</a></li>
<li><a href="#">Page 1-2</a></li>
<li><a href="#">Page 1-3</a></li>
</ul>
</li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="main container-fluid">
<div class="row">
<div class="sidebar sidebar-left col-xs-12 col-sm-4 col-md-3">
<ul class="list-group">
<li class="list-group-item"><a href="#">First item</a></li>
<li class="list-group-item"><a href="#">Second item</a></li>
<li class="list-group-item"><a href="#">Third item</a></li>
</ul>
</div>
<div class="content col-xs-12">
<h2 class="mt-0">Lorem ipsum dolor</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi nulla optio, repudiandae laborum dicta molestias adipisci tempore aspernatur magni fugit corporis aliquid reiciendis, suscipit aut, esse, hic possimus explicabo dolorem!</p>
<p>Quasi temporibus error, amet consequuntur reprehenderit veniam? Molestiae quos praesentium facilis at repellat iusto maiores porro est, reiciendis, repellendus nam dolores necessitatibus?</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt nam expedita sint error eos voluptas explicabo, sequi quidem eum, velit vitae autem dolorum vero nulla doloremque beatae enim veritatis ipsum.</p>
</div>
<div class="sidebar sidebar-right col-xs-12 col-sm-4 col-md-3">
<ul class="list-group">
<li class="list-group-item"><a href="#">First item</a></li>
<li class="list-group-item"><a href="#">Second item</a></li>
<li class="list-group-item"><a href="#">Third item</a></li>
</ul>
</div>
</div>
</div>
<footer class="site-footer">
<p class="text-center">© 2019 @My Company</p>
</footer>
</div>
答案 2 :(得分:0)
如果字符串的长度为奇数,则可以计算索引并采用实际索引的这个字符。
function flipPairs(string) {
return [...string].map((c, i, a) => a[i + ((i + 1) % 2 || -1)] || c).join('');
}
console.log(flipPairs('hello world'));