如何通过重复相同的单词创建与另一个匹配的字符串? [英] How do I create a string matching another by repeating the same word?

查看:69
本文介绍了如何通过重复相同的单词创建与另一个匹配的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找一个重复相同单词的函数,直到它与输入消息的长度相匹配。



示例:输入是我喜欢旅游并获得纪念品。

输出将是bananapiecreambananapiecreambananap

所以我的密钥的每个字符都会匹配输入消息中的字符,包括空格和标点符号。



我尝试过: < br $>


I am trying to look for a function that would repeat the same word until it matches the length of the input message.

Example: input is "I love to travel and get souvenirs."
output would be "bananapiecreambananapiecreambananap"
so each character of my key would match a character from the input message including spaces and punctuation.

What I have tried:

var str = "bananapie";
var endingW = "cream";
var codeKey = str.concat(endingW);
var input = prompt("input message to transform");





我从这里迷失了。



I am pretty lost from here.

推荐答案

尝试

try
var input = "Hello World!";
var key = "foo42";
var output = "";
var keylen = key.length;
for (var i = 0, len = input.length; i < len; i++) {
  output += key[ i % keylen];
}
alert(output);


这篇关于如何通过重复相同的单词创建与另一个匹配的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆