JavaScript:遍历字符串并返回字符串+'ay'(如果字符串的首字母中包含元音)

时间:2019-05-23 18:39:18

标签: javascript string loops return slice

我正在尝试运行一个可以做猪拉丁的功能。我陷入了第一步。

当我将输入参数设置为'eat'=>时,我希望我的代码返回字符串加'ay'。请参见下面的代码以及if语句。

相反,当我运行我的代码时,它返回未定义。我希望它返回“进餐”。为什么?

if (arr1[i][0] == arr2[j][0] && arr1[i][1] == arr2[j][1])

2 个答案:

答案 0 :(得分:2)

您有<!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <title>Alternative for document.getElementById</title> <script> /* I had made a slight error with the selector - it should be input.button1 rather than button.button1 The action invoked by the button also needed to be changed - it now navigates up the DOM tree to it's parent then to the parent's sibling */ document.addEventListener('DOMContentLoaded',()=>{ Array.prototype.slice.call( document.querySelectorAll( 'div.overlay, input.button1' ) ).forEach( function(node){ node.addEventListener('click', function(e){ e.preventDefault(); switch( this.tagName ){ case 'INPUT': this.parentNode.nextElementSibling.style.display='block'; break; case 'DIV': this.style.display='none'; break; } }); }); }); </script> <style> html *{box-sizing:border-box;} #wrapper{width:100%;height:100vh;margin:0;padding:0;} #wrapper > a.annons-tb{ padding:1rem;height:5rem;width:100%;display:inline-block; } #wrapper > div.overlay{ background:whitesmoke;border:1px dotted rgba(100,100,100,0.25);padding:1rem;margin:1rem auto;display:none } </style> </head> <body> <div id='wrapper'> <?php /* To emulate the db call a static array can be used */ $rs=array( array( 'titleGallery'=>'Mega Gallery 1', 'descGallery'=>'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed orci ipsum, commodo pulvinar rutrum non, congue ut justo. Phasellus nec scelerisque diam, ut cursus felis.', 'imgFullNameGallery'=>'Gallery 1', 'prisGallery'=>'abc', 'namnGallery'=>'bob', 'emailGallery'=>'gal1@example.com', 'nummerGallery'=>'0141 353 3780' ), array( 'titleGallery'=>'Super Gallery 2', 'descGallery'=>'urabitur tristique libero non lorem blandit eleifend. Sed gravida ut purus vitae dignissim. Aenean pellentesque, diam ac euismod eleifend, lorem mauris dapibus lorem, id fermentum augue nisi at nisl.', 'imgFullNameGallery'=>'Gallery 2', 'prisGallery'=>'def', 'namnGallery'=>'rita', 'emailGallery'=>'gal2@example.com', 'nummerGallery'=>'0131 551 7488' ), array( 'titleGallery'=>'Awesome Gallery 3', 'descGallery'=>'Morbi ultrices nisl elementum augue sagittis porttitor. Nulla posuere molestie quam posuere tempus. Sed convallis metus mi, quis faucibus est lobortis sed.', 'imgFullNameGallery'=>'Gallery 3', 'prisGallery'=>'ghi', 'namnGallery'=>'sue', 'emailGallery'=>'gal3@example.com', 'nummerGallery'=>'01307 462 059' ) ); /* for the static array a foreach rather than while loop but essentially the same... I prefer use printf to constantly escaping out from the html to include the variable but again essentially the same as the original except for the lack of ID attributes */ foreach( $rs as $row ){ printf(' <a class="annons-tb"> <div style="background-image: url(img/gallery/%s);"></div> <h3>%s</h3> <p>%s</p> <input type="button" class="button1" name="id" value="Visa Mer Info"> </a> <div class="overlay"> <div class="text"> <img src="data:img/gallery/jpeg;base64,%s" width="250" height="250" /> <h3>%s</h3> <p1>Beskrivning: %s</p1> <div class="p-delen"> <br /> <p>Pris: %s</p> <p>Namn: %s</p> <p>Mail: %s</p> <p>TelefonNummer: %s</p> </div> </div> </div>', $row['imgFullNameGallery'], $row['titleGallery'], $row['descGallery'], $row['imgFullNameGallery'], $row['titleGallery'], $row['descGallery'], $row['prisGallery'], $row['namnGallery'], $row['emailGallery'], $row['nummerGallery'] ); } ?> </div> </body> </html> 错误的论点。语法为includes(),因此应为:

container.includes(element)

答案 1 :(得分:1)

JavaScript本身提供了“ includes”和“ charAt”功能。可以使用str.charAt(0)代替将字符串拆分为字符数组并找到第一个元素。并包括搜索数组的函数,如果有任何元素匹配,则返回true。

const pigify = (str) => {
  if(['a', 'e', 'i', 'o', 'u'].includes(str.charAt(0))) {
  return str+'ay';
}