简体   繁体   English

如何在 javascript 中的每个单词后添加一个符号

[英]How to add a symbol after every word in javascript

So my code currently only puts -boink or -bork after the entire string once the conditions are met but I want it so that after every single word the terms get added based on whether or not it satisfies the conditions of being either greater or less than five characters.因此,我的代码目前仅在满足条件后将 -boink 或 -bork 放在整个字符串之后,但我想要它以便在每个单词之后根据它是否满足大于或小于的条件来添加术语五个字符。 For example "My-boink name-boinkk is-boink Emmanuel-bork"例如“My-boink name-boinkk is-boink Emmanuel-bork”

function myFunctionOne(input1, input2) {

var prompt1 = prompt("Please enter 1, 2, 3, or Exit").toLowerCase();
var prompt2 = input2;

if (prompt1 == 1) {
prompt2 = prompt("Please enter a string");

while (prompt2.length === 0) {
  prompt2 = prompt("You need to enter something");
}

myFunctionOne(prompt1, prompt2);
}

if (prompt1 == 2) {
  if (prompt2.length > 5) {
      console.log(prompt2 + "-bork");
  }
  myFunctionOne(prompt2);
}
else {
  console.log(prompt2 + "-boink")
}

}
 myFunctionOne(1, 2, null);

You need to split the string into words with the split method and then loop through them using a for loop to check if they're longer than 5 characters and add 'bork' or 'boink' and then join the words again.您需要使用 split 方法将字符串拆分为单词,然后使用 for 循环遍历它们以检查它们是否超过 5 个字符并添加“bork”或“boink”,然后再次加入单词。

I could write the code for you, but I think it will be more satisfying for you to do it yourself.我可以为您编写代码,但我认为您自己编写代码会更令人满意。 If you want me to write it thought let me know.如果你想让我写它想告诉我。

Edit I'm going to write the code as close as what you already have as posible编辑我将编写尽可能接近您已有的代码

function myFunctionOne(input1, input2) {

    var prompt1 = prompt("Please enter 1, 2, 3, or Exit").toLowerCase();
    var prompt2 = input2;

    if (prompt1 == 1) {
        prompt2 = prompt("Please enter a string");

        while (prompt2.length === 0) {
            prompt2 = prompt("You need to enter something");
        }

        myFunctionOne(prompt1, prompt2);
    }

    if (prompt1 == 2) {
        var words = prompt2.split(" "); // we separate the string into words dividing them by space into an array called words
        for(var i = 0; i < words.length; i++){ // we loop from 0 to the length of the array - 1 to access all positions in the array (the last position in arrays are always the length of the array - 1 because they start at 0)  
            if(words[i].length > 5){ //we check if the word in this position of the array is longer than 5 characters
                words[i] += "-bork"; //if it is we add -bork
            }else{
                words[i] += "-boink" //if it is not longer than 5 characters we add -boink
            }
        }
        console.log(words.join(" ")); //we print the array joining the elements with an space to form a string
    }

}
myFunctionOne(1, 2, null);

I'm a little confused by what's going on at the top of your code, so I'm not going to refactor the entire thing.我对代码顶部发生的事情有点困惑,所以我不打算重构整个事情。 I'm going to provide an explanation from the time we have our string.从我们得到字符串的时候起,我将提供一个解释。

One approach would be to use.split(), which will return an array of string values based on what character you choose to split the string by.一种方法是使用.split(),它将根据您选择分割字符串的字符返回一个字符串值数组。 The reason we need to do this is because your code is currently looping through each string, rather than each word in the string.我们需要这样做的原因是因为您的代码当前正在循环遍历每个字符串,而不是字符串中的每个单词。 In this instance, I'm assuming your string can't take punctuation like commas or periods.在这种情况下,我假设您的字符串不能使用逗号或句点等标点符号。 If that's the case, you want to split by empty spaces, so it would look like string.split(" ").如果是这种情况,你想用空格分割,所以它看起来像 string.split(" ")。

Then, you could use the map() array method to loop through every value in the array and perform a function on it.然后,您可以使用 map() 数组方法循环遍历数组中的每个值并对其执行 function。 Note, the map() method will return a new array, so best to save this into a new variable.请注意,map() 方法将返回一个新数组,因此最好将其保存到一个新变量中。

Then, you could use the.join() method, which will join the values of an array based on some value (essentially the opposite of.split()).然后,您可以使用 .join() 方法,该方法将基于某个值连接数组的值(本质上与 .split() 相反)。 Again, assuming no punctuation, I'd join the array with a space so that the values have a space between them, which would look like array.join(" ").同样,假设没有标点符号,我会用空格连接数组,以便值之间有空格,看起来像 array.join(" ")。

I've included some mock code below.我在下面包含了一些模拟代码。

const string = prompt("Please enter your string");

const stringToArray = string.split(" ");
console.log(stringToArray);

const filteredArray = stringToArray.map((string) => {
  if (string.length > 5) {
    return string + "-bork";
  }

  return string + "-boink";
});
console.log(filteredArray);

const joinedFilteredArray = filteredArray.join(" ");
console.log(joinedFilteredArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM