根据字符串中的标点将字符串拆分为数组元素-JavaScript [英] Split String Into Array Elements Based On Punctuation In String - JavaScript

查看:56
本文介绍了根据字符串中的标点将字符串拆分为数组元素-JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按句子将字符串拆分成单独的数组元素 .因此,每个周期后都需要进行拆分.

即这个:

 爱是燃烧的东西,它会发出火热的戒指.受狂野欲望的束缚,我陷入了火圈. 

会变成这样:

  ['爱是燃烧的东西,它制造了火热的戒指.',被狂野的欲望所束缚,我掉进了火圈",] 

在这种情况下,

Array [0] 将是爱是燃烧的东西,它会发出火热的戒指." .

我尝试过:

  function splitLyrics(string){让newArray = [];for(让i = 0; i< string.length; i ++){let character = string [i];让句子=";if(字符!==."){句子.concat(字符);}newArray.push(sentence);}返回newArray;}console.log(splitLyrics(爱是燃烧的东西,它制造了火热的戒指.由于野心勃勃的欲望,我陷入了火环.")); 

正如您所看到的,我的问题是我不知道如何将各个字符元素组合成句子元素,并在句号之间将其分隔开.

按照我现有的逻辑,有哪些功能可以实现我正在寻找的功能?

https://www.codewars.com/kata/99-bottles-of-beer-1/train/javascript

解决方案

您可以使用正则表达式和 .match 代替 - 匹配一个非空格字符,然后是非句点字符,然后是按句点字符:

  const splitLyrics = str =>str.match(/\ S [^.] * \ ./g);console.log(splitLyrics(爱是燃烧的东西,它发出火热的戒指.由于野心勃勃的欲望,我跌入了火环."));  

这也很容易扩展到其他以句子结尾的标点符号,例如感叹号和问号:

  const splitLyrics = str =>str.match(/\ S [^.?!] * [.?!]/g);console.log(splitLyrics(爱是燃烧的东西,它发出火热的戒指.被狂野的欲望所束缚,我跌入了火环./pre>

如果需要 var song = sing(); ,并且测试人员继续调用 song ,那么您应该编写一个返回函数,尽管这似乎很奇怪:

  const sing =()=>str =>str.match(/\ S [^.] * \ ./g);const song = sing();console.log(song(爱是燃烧的东西,它发出火热的戒指.被狂野的欲望所束缚,我跌入了火环."));  

看来kata希望您粘贴整首歌的歌词,然后返回相应的行:

您可以在此处找到完整的歌词.

那么要么这样做:

 //snippet实际上不是可运行的,但是文本太多,无法正确地粘贴到答案中const sing =()=>`99瓶啤酒在墙上,99瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满98瓶啤酒.墙上有98瓶啤酒,有98瓶啤酒.放下一瓶,然后将其传递到墙壁上,装满97瓶啤酒.墙上有97瓶啤酒,有97瓶啤酒.取下一瓶,然后将其传递到墙壁上的96瓶啤酒中.墙上有96瓶啤酒,有96瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满95瓶啤酒.墙上有95瓶啤酒,有95瓶啤酒.放下一瓶,将其传递到墙壁上的94瓶啤酒中.墙上有94瓶啤酒,有94瓶啤酒.放下一瓶,将其传递到墙壁上的93瓶啤酒中.墙上有93瓶啤酒,有93瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满92瓶啤酒.墙上92瓶啤酒,92瓶啤酒.放下一瓶,将其传递到墙壁上,装满91瓶啤酒.墙上有91瓶啤酒,有91瓶啤酒.取下一个并传递给它,墙上有 90 瓶啤酒.墙上有90瓶啤酒,有90瓶啤酒.放下一瓶,然后将其传递到墙壁上,装满89瓶啤酒.墙上有89瓶啤酒,有89瓶啤酒.放下一瓶,然后将其传递到墙壁上的88瓶啤酒中.墙上有88瓶啤酒,有88瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满87瓶啤酒.墙上有87瓶啤酒,有87瓶啤酒.取下一瓶,然后将其传递到墙壁上的86瓶啤酒中.墙上有86瓶啤酒,有86瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满85瓶啤酒.墙上有85瓶啤酒,有85瓶啤酒.放下一瓶,然后将其通过,然后将84瓶啤酒放在墙上.84瓶啤酒在墙上,84瓶啤酒.取下一瓶,然后将其传递到墙壁上的83瓶啤酒中.墙上有83瓶啤酒,还有83瓶啤酒.倒下一瓶,然后将其传递到墙壁上的82瓶啤酒中.墙上有82瓶啤酒,有82瓶啤酒.取下一瓶,然后将其传递到墙壁上的81瓶啤酒中.墙上有81瓶啤酒,有81瓶啤酒.放下一瓶,将其传递到墙壁上,装满80瓶啤酒.墙上有80瓶啤酒,有80瓶啤酒.取下来一瓶,然后将其传递到墙壁上,装满79瓶啤酒.墙上有79瓶啤酒,有79瓶啤酒.放下一瓶,将其通过,将78瓶啤酒放在墙上.墙上有78瓶啤酒,有78瓶啤酒.放下一瓶,将其传递到墙壁上,装满77瓶啤酒.墙上有77瓶啤酒,有77瓶啤酒.拿下一个,然后把它传出去,墙上有 76 瓶啤酒.墙上有76瓶啤酒,有76瓶啤酒.取下一瓶,然后将其传递到墙壁上的75瓶啤酒中.墙上有75瓶啤酒,有75瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满74瓶啤酒.墙上有74瓶啤酒,有74瓶啤酒.取下一瓶,然后将其传递到墙壁上,一瓶73瓶啤酒.墙上的啤酒73瓶,啤酒的73瓶.放下一瓶,将其传递到墙壁上,装满72瓶啤酒.墙上有72瓶啤酒,有72瓶啤酒.倒下一瓶,然后将其传递到墙壁上的71瓶啤酒中.墙上的啤酒71瓶,啤酒的71瓶.放下一瓶,将其传递到墙壁上,装满70瓶啤酒.墙上有70瓶啤酒,有70瓶啤酒.倒下一瓶,将其传递到墙壁上,装满69瓶啤酒.墙上有69瓶啤酒,有69瓶啤酒.放下一瓶,然后将其传递到墙壁上,装满68瓶啤酒.墙上有68瓶啤酒,有68瓶啤酒.倒下一瓶,然后将其传递到墙壁上的67瓶啤酒中.墙上有67瓶啤酒,有67瓶啤酒.放下一瓶,将其传递到墙壁上的66瓶啤酒中.墙上有66瓶啤酒,有66瓶啤酒.放下一瓶,将其传递到墙壁上,一共65瓶啤酒.墙上有65瓶啤酒,有65瓶啤酒.取下一瓶,然后将其传递到墙壁上的64瓶啤酒中.墙上有64瓶啤酒,有64瓶啤酒.放下一瓶,将其传递到墙壁上的63瓶啤酒中.墙上63瓶啤酒,63瓶啤酒.倒下一瓶,然后将其传递到墙壁上的62瓶啤酒中.墙上的啤酒62瓶,啤酒的62瓶.倒下一瓶,然后将其传递到墙壁上的61瓶啤酒中.墙上有61瓶啤酒,有61瓶啤酒.放下一瓶,将其传递到墙壁上,装满60瓶啤酒.墙上有60瓶啤酒,有60瓶啤酒.放下一只,然后将其通过,然后将59瓶啤酒放在墙上.墙上有59瓶啤酒,有59瓶啤酒.放下一罐,将其通过,然后将58瓶啤酒放在墙上.墙上有58瓶啤酒,有58瓶啤酒.取下一瓶,然后将其通过,在墙上放57瓶啤酒.墙上有57瓶啤酒,有57瓶啤酒.放下一瓶,将其传递到墙壁上的56瓶啤酒中.墙上有56瓶啤酒,有56瓶啤酒.取下一瓶,然后将其通过,将55瓶啤酒放在墙上.墙上有55瓶啤酒,有55瓶啤酒.取下一瓶,然后将其传递到墙壁上的54瓶啤酒中.墙上有54瓶啤酒,有54瓶啤酒.倒下一瓶,然后将其传递到墙壁上的53瓶啤酒中.墙上有53瓶啤酒,还有53瓶啤酒.放下一瓶,将其传递到墙壁上的52瓶啤酒中.墙上有52瓶啤酒,有52瓶啤酒.放下一瓶,将其传递到墙壁上的51瓶啤酒中.墙上有51瓶啤酒,有51瓶啤酒.取下一瓶,然后将其传递到墙壁上的50瓶啤酒中.墙上有50瓶啤酒,50瓶啤酒.取下一瓶,然后将其传递到墙壁上的49瓶啤酒中.墙上的啤酒49瓶,啤酒的49瓶.放下一瓶,将其传递到墙壁上的48瓶啤酒中.墙上有48瓶啤酒,有48瓶啤酒.取下一瓶,然后将其传递到墙壁上,一瓶47瓶啤酒.墙上有47瓶啤酒,有47瓶啤酒.放下一瓶,将其传递到墙壁上的46瓶啤酒中.墙上有46瓶啤酒,有46瓶啤酒.取下一瓶,然后将其传递到墙壁上的45瓶啤酒中.墙上有45瓶啤酒,有45瓶啤酒.放下一瓶,然后将其传递到墙壁上的44瓶啤酒中.墙上有44瓶啤酒,有44瓶啤酒.取下一瓶,然后将其传递到墙壁上的43瓶啤酒中.墙上有43瓶啤酒,有43瓶啤酒.放下一瓶,将其传递到墙壁上的42瓶啤酒中.墙上有42瓶啤酒,有42瓶啤酒.放下一瓶,将其传递到墙壁上的41瓶啤酒中.墙上有41瓶啤酒,有41瓶啤酒.放下一瓶,将其传递到墙壁上,装满40瓶啤酒.墙上有40瓶啤酒,有40瓶啤酒.取下一瓶,然后将其传递到墙壁上,一瓶39瓶啤酒.墙上有39瓶啤酒,还有39瓶啤酒.放下一瓶,将其传递到墙壁上,装满38瓶啤酒.墙上有38瓶啤酒,有38瓶啤酒.放下一瓶,将其传递到墙壁上,一瓶37瓶啤酒.墙上有37瓶啤酒,有37瓶啤酒.放下一瓶,将其传递到墙壁上,装满36瓶啤酒.墙上有36瓶啤酒,有36瓶啤酒.放下一瓶,将其传递到墙壁上的35瓶啤酒中.墙上有35瓶啤酒,有35瓶啤酒.取下一瓶,然后将其传递到墙壁上的34瓶啤酒中.墙上有34瓶啤酒,有34瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满33瓶啤酒.墙上有33瓶啤酒,有33瓶啤酒.取下一瓶,然后将其传递到墙壁上的32瓶啤酒中.墙上有32瓶啤酒,有32瓶啤酒.取一瓶下来,将其传递到墙壁上的31瓶啤酒中.墙上的31瓶啤酒,31瓶啤酒.取下一个并传递它,墙上有 30 瓶啤酒.墙上有30瓶啤酒,有30瓶啤酒.放下一瓶,将其传递到墙壁上,一共29瓶啤酒.墙上有29瓶啤酒,有29瓶啤酒.放下一瓶,将其传递到墙壁上的28瓶啤酒中.墙上有28瓶啤酒,有28瓶啤酒.放下一瓶,然后将其传递到墙壁上的27瓶啤酒中.墙上有27瓶啤酒,有27瓶啤酒.放下一瓶,将其传递到墙壁上的26瓶啤酒中.墙上有26瓶啤酒,有26瓶啤酒.取下一瓶,然后将其传递到墙壁上的25瓶啤酒中.墙上有25瓶啤酒,有25瓶啤酒.取下一个并传递给墙上的 24 瓶啤酒.墙上有24瓶啤酒,有24瓶啤酒.放下一瓶,将其传递到墙壁上,一瓶23瓶啤酒.墙上有23瓶啤酒,有23瓶啤酒.放下一瓶,将其传递到墙壁上,装满22瓶啤酒.墙上有22瓶啤酒,有22瓶啤酒.放下一瓶,将其传递到墙壁上,装满21瓶啤酒.墙上有21瓶啤酒,有21瓶啤酒.放下一瓶,将其传递到墙壁上,一瓶20瓶啤酒.墙上有20瓶啤酒,有20瓶啤酒.放下一只,然后将其通过,墙上放19瓶啤酒.墙上有19瓶啤酒,有19瓶啤酒.放下一只,然后将它传递出去,墙上放18瓶啤酒.墙上有18瓶啤酒,有18瓶啤酒.放下一只,然后将其通过,墙上放17瓶啤酒.墙上有17瓶啤酒,有17瓶啤酒.放下一瓶,将其传递到墙壁上,装满16瓶啤酒.墙上有16瓶啤酒,有16瓶啤酒.放下一瓶,将其传递到墙壁上,装满15瓶啤酒.墙上有15瓶啤酒,还有15瓶啤酒.取一瓶下来,将其传递到墙壁上的14瓶啤酒中.墙上有14瓶啤酒,有14瓶啤酒.取下一瓶,然后将其传递到墙壁上,装满13瓶啤酒.墙上有13瓶啤酒,13瓶啤酒.放下一只,然后将它传递到墙壁上,装满12瓶啤酒.墙上有12瓶啤酒,有12瓶啤酒.放下一瓶,将其通过,墙上放11瓶啤酒.墙上有11瓶啤酒,有11瓶啤酒.放下一只,然后将其传递,将10瓶啤酒放在墙上.墙上有10瓶啤酒,有10瓶啤酒.取一瓶下来,将其传递到墙壁上,装9瓶啤酒.墙上有9瓶啤酒,有9瓶啤酒.取一瓶下来,将其通过,墙上放8瓶啤酒.墙上有8瓶啤酒,有8瓶啤酒.放下一只,将其通过,墙上放7瓶啤酒.墙上有7瓶啤酒,有7瓶啤酒.放下一瓶,将其通过,然后将6瓶啤酒放在墙上.墙上有6瓶啤酒,有6瓶啤酒.拿下一个然后传过去,墙上有 5 瓶啤酒.墙上有5瓶啤酒,还有5瓶啤酒.放下一只,将其通过,将4瓶啤酒放在墙上.墙上有4瓶啤酒,有4瓶啤酒.放下一瓶,将其传递到墙壁上,装满3瓶啤酒.3瓶啤酒在墙上,3瓶啤酒.放下一只,将其通过,将2瓶啤酒放在墙上.2瓶啤酒在墙上,2瓶啤酒.放下一罐,将其通过,然后将1瓶啤酒放在墙上.1瓶啤酒在墙上,1瓶啤酒.放下一瓶,把它传递过去,墙上不再有啤酒瓶.墙上不再有啤酒瓶,不再有啤酒瓶.去商店,在墙上买更多的99瓶啤酒.`.split('\ n').filter(Boolean) 

或者,对于这个非常重复的 歌曲",您可以自己创建歌词,虽然可能不是很清楚,但这可能是练习的重点:

  const sing =()=>[... Array.from({长度:98},(_,i)=>[`$ {99-i}瓶啤酒在墙上,$ {99-i}瓶啤酒.把一瓶下来,传给我,$ {99-i-1}瓶啤酒在墙上.]).平坦的(),`1瓶啤酒在墙上,1瓶啤酒.`下来一并传递,墙上再也没有啤酒瓶了.`,`墙上不再有啤酒瓶,啤酒也没有瓶.`去商店,在墙上购买更多的99瓶啤酒.];const song = sing();console.log(song [0] ==='墙上有99瓶啤酒,有99瓶啤酒.');  

I want to split a string into separate array elements by sentence. So a split needs to occur after every period.

I.e. this:

Love is a burning thing and it makes a fiery ring.
Bound by wild desire, I fell in to a ring of fire.

Will become this:

[ 'Love is a burning thing and it makes a fiery ring.',
 'Bound by wild desire, I fell into a ring of fire', ]

Array[0] in this case will be "Love is a burning thing and it makes a fiery ring.".

I tried this:

function splitLyrics(string) {
  let newArray = [];

  for (let i = 0; i < string.length; i++) {
    let character = string[i];
    let sentence = "";

    if (character !== ".") {
      sentence.concat(character);
    }

    newArray.push(sentence);

  }

  return newArray;

}

console.log(splitLyrics("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire."));

And as you can see, my problem is that I don't know how to combine the individual character elements into sentence elements, separated by when there's a period.

What functions are out there that could achieve what I'm looking for, following my existing logic?

https://www.codewars.com/kata/99-bottles-of-beer-1/train/javascript

解决方案

You might use a regular expression and .match instead - match a non-space character, followed by non-period characters, followed by a period character:

const splitLyrics = str => str.match(/\S[^.]*\./g);

console.log(splitLyrics("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire."));

This is easily extendible to other sentence-terminating punctuation too, like exclamation and question marks:

const splitLyrics = str => str.match(/\S[^.?!]*[.?!]/g);

console.log(splitLyrics("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire. Foo bar! Bar baz? Buzz."));

If var song = sing(); is required, and the tester proceeds to call song, then you should write a function that returns a function, though this seems like a very odd thing to do:

const sing = () => str => str.match(/\S[^.]*\./g);
const song = sing();
console.log(song("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire."));

It appears that the kata is expecting you to paste in the entire lyrics for the song and then return the appropriate line:

You can find the complete lyrics here.

So either do that:

// snippet isn't actually runnable, but it's too much text to paste in the answer proper

const sing = () => `99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.

96 bottles of beer on the wall, 96 bottles of beer.
Take one down and pass it around, 95 bottles of beer on the wall.

95 bottles of beer on the wall, 95 bottles of beer.
Take one down and pass it around, 94 bottles of beer on the wall.

94 bottles of beer on the wall, 94 bottles of beer.
Take one down and pass it around, 93 bottles of beer on the wall.

93 bottles of beer on the wall, 93 bottles of beer.
Take one down and pass it around, 92 bottles of beer on the wall.

92 bottles of beer on the wall, 92 bottles of beer.
Take one down and pass it around, 91 bottles of beer on the wall.

91 bottles of beer on the wall, 91 bottles of beer.
Take one down and pass it around, 90 bottles of beer on the wall.

90 bottles of beer on the wall, 90 bottles of beer.
Take one down and pass it around, 89 bottles of beer on the wall.

89 bottles of beer on the wall, 89 bottles of beer.
Take one down and pass it around, 88 bottles of beer on the wall.

88 bottles of beer on the wall, 88 bottles of beer.
Take one down and pass it around, 87 bottles of beer on the wall.

87 bottles of beer on the wall, 87 bottles of beer.
Take one down and pass it around, 86 bottles of beer on the wall.

86 bottles of beer on the wall, 86 bottles of beer.
Take one down and pass it around, 85 bottles of beer on the wall.

85 bottles of beer on the wall, 85 bottles of beer.
Take one down and pass it around, 84 bottles of beer on the wall.

84 bottles of beer on the wall, 84 bottles of beer.
Take one down and pass it around, 83 bottles of beer on the wall.

83 bottles of beer on the wall, 83 bottles of beer.
Take one down and pass it around, 82 bottles of beer on the wall.

82 bottles of beer on the wall, 82 bottles of beer.
Take one down and pass it around, 81 bottles of beer on the wall.

81 bottles of beer on the wall, 81 bottles of beer.
Take one down and pass it around, 80 bottles of beer on the wall.

80 bottles of beer on the wall, 80 bottles of beer.
Take one down and pass it around, 79 bottles of beer on the wall.

79 bottles of beer on the wall, 79 bottles of beer.
Take one down and pass it around, 78 bottles of beer on the wall.

78 bottles of beer on the wall, 78 bottles of beer.
Take one down and pass it around, 77 bottles of beer on the wall.

77 bottles of beer on the wall, 77 bottles of beer.
Take one down and pass it around, 76 bottles of beer on the wall.

76 bottles of beer on the wall, 76 bottles of beer.
Take one down and pass it around, 75 bottles of beer on the wall.

75 bottles of beer on the wall, 75 bottles of beer.
Take one down and pass it around, 74 bottles of beer on the wall.

74 bottles of beer on the wall, 74 bottles of beer.
Take one down and pass it around, 73 bottles of beer on the wall.

73 bottles of beer on the wall, 73 bottles of beer.
Take one down and pass it around, 72 bottles of beer on the wall.

72 bottles of beer on the wall, 72 bottles of beer.
Take one down and pass it around, 71 bottles of beer on the wall.

71 bottles of beer on the wall, 71 bottles of beer.
Take one down and pass it around, 70 bottles of beer on the wall.

70 bottles of beer on the wall, 70 bottles of beer.
Take one down and pass it around, 69 bottles of beer on the wall.

69 bottles of beer on the wall, 69 bottles of beer.
Take one down and pass it around, 68 bottles of beer on the wall.

68 bottles of beer on the wall, 68 bottles of beer.
Take one down and pass it around, 67 bottles of beer on the wall.

67 bottles of beer on the wall, 67 bottles of beer.
Take one down and pass it around, 66 bottles of beer on the wall.

66 bottles of beer on the wall, 66 bottles of beer.
Take one down and pass it around, 65 bottles of beer on the wall.

65 bottles of beer on the wall, 65 bottles of beer.
Take one down and pass it around, 64 bottles of beer on the wall.

64 bottles of beer on the wall, 64 bottles of beer.
Take one down and pass it around, 63 bottles of beer on the wall.

63 bottles of beer on the wall, 63 bottles of beer.
Take one down and pass it around, 62 bottles of beer on the wall.

62 bottles of beer on the wall, 62 bottles of beer.
Take one down and pass it around, 61 bottles of beer on the wall.

61 bottles of beer on the wall, 61 bottles of beer.
Take one down and pass it around, 60 bottles of beer on the wall.

60 bottles of beer on the wall, 60 bottles of beer.
Take one down and pass it around, 59 bottles of beer on the wall.

59 bottles of beer on the wall, 59 bottles of beer.
Take one down and pass it around, 58 bottles of beer on the wall.

58 bottles of beer on the wall, 58 bottles of beer.
Take one down and pass it around, 57 bottles of beer on the wall.

57 bottles of beer on the wall, 57 bottles of beer.
Take one down and pass it around, 56 bottles of beer on the wall.

56 bottles of beer on the wall, 56 bottles of beer.
Take one down and pass it around, 55 bottles of beer on the wall.

55 bottles of beer on the wall, 55 bottles of beer.
Take one down and pass it around, 54 bottles of beer on the wall.

54 bottles of beer on the wall, 54 bottles of beer.
Take one down and pass it around, 53 bottles of beer on the wall.

53 bottles of beer on the wall, 53 bottles of beer.
Take one down and pass it around, 52 bottles of beer on the wall.

52 bottles of beer on the wall, 52 bottles of beer.
Take one down and pass it around, 51 bottles of beer on the wall.

51 bottles of beer on the wall, 51 bottles of beer.
Take one down and pass it around, 50 bottles of beer on the wall.

50 bottles of beer on the wall, 50 bottles of beer.
Take one down and pass it around, 49 bottles of beer on the wall.

49 bottles of beer on the wall, 49 bottles of beer.
Take one down and pass it around, 48 bottles of beer on the wall.

48 bottles of beer on the wall, 48 bottles of beer.
Take one down and pass it around, 47 bottles of beer on the wall.

47 bottles of beer on the wall, 47 bottles of beer.
Take one down and pass it around, 46 bottles of beer on the wall.

46 bottles of beer on the wall, 46 bottles of beer.
Take one down and pass it around, 45 bottles of beer on the wall.

45 bottles of beer on the wall, 45 bottles of beer.
Take one down and pass it around, 44 bottles of beer on the wall.

44 bottles of beer on the wall, 44 bottles of beer.
Take one down and pass it around, 43 bottles of beer on the wall.

43 bottles of beer on the wall, 43 bottles of beer.
Take one down and pass it around, 42 bottles of beer on the wall.

42 bottles of beer on the wall, 42 bottles of beer.
Take one down and pass it around, 41 bottles of beer on the wall.

41 bottles of beer on the wall, 41 bottles of beer.
Take one down and pass it around, 40 bottles of beer on the wall.

40 bottles of beer on the wall, 40 bottles of beer.
Take one down and pass it around, 39 bottles of beer on the wall.

39 bottles of beer on the wall, 39 bottles of beer.
Take one down and pass it around, 38 bottles of beer on the wall.

38 bottles of beer on the wall, 38 bottles of beer.
Take one down and pass it around, 37 bottles of beer on the wall.

37 bottles of beer on the wall, 37 bottles of beer.
Take one down and pass it around, 36 bottles of beer on the wall.

36 bottles of beer on the wall, 36 bottles of beer.
Take one down and pass it around, 35 bottles of beer on the wall.

35 bottles of beer on the wall, 35 bottles of beer.
Take one down and pass it around, 34 bottles of beer on the wall.

34 bottles of beer on the wall, 34 bottles of beer.
Take one down and pass it around, 33 bottles of beer on the wall.

33 bottles of beer on the wall, 33 bottles of beer.
Take one down and pass it around, 32 bottles of beer on the wall.

32 bottles of beer on the wall, 32 bottles of beer.
Take one down and pass it around, 31 bottles of beer on the wall.

31 bottles of beer on the wall, 31 bottles of beer.
Take one down and pass it around, 30 bottles of beer on the wall.

30 bottles of beer on the wall, 30 bottles of beer.
Take one down and pass it around, 29 bottles of beer on the wall.

29 bottles of beer on the wall, 29 bottles of beer.
Take one down and pass it around, 28 bottles of beer on the wall.

28 bottles of beer on the wall, 28 bottles of beer.
Take one down and pass it around, 27 bottles of beer on the wall.

27 bottles of beer on the wall, 27 bottles of beer.
Take one down and pass it around, 26 bottles of beer on the wall.

26 bottles of beer on the wall, 26 bottles of beer.
Take one down and pass it around, 25 bottles of beer on the wall.

25 bottles of beer on the wall, 25 bottles of beer.
Take one down and pass it around, 24 bottles of beer on the wall.

24 bottles of beer on the wall, 24 bottles of beer.
Take one down and pass it around, 23 bottles of beer on the wall.

23 bottles of beer on the wall, 23 bottles of beer.
Take one down and pass it around, 22 bottles of beer on the wall.

22 bottles of beer on the wall, 22 bottles of beer.
Take one down and pass it around, 21 bottles of beer on the wall.

21 bottles of beer on the wall, 21 bottles of beer.
Take one down and pass it around, 20 bottles of beer on the wall.

20 bottles of beer on the wall, 20 bottles of beer.
Take one down and pass it around, 19 bottles of beer on the wall.

19 bottles of beer on the wall, 19 bottles of beer.
Take one down and pass it around, 18 bottles of beer on the wall.

18 bottles of beer on the wall, 18 bottles of beer.
Take one down and pass it around, 17 bottles of beer on the wall.

17 bottles of beer on the wall, 17 bottles of beer.
Take one down and pass it around, 16 bottles of beer on the wall.

16 bottles of beer on the wall, 16 bottles of beer.
Take one down and pass it around, 15 bottles of beer on the wall.

15 bottles of beer on the wall, 15 bottles of beer.
Take one down and pass it around, 14 bottles of beer on the wall.

14 bottles of beer on the wall, 14 bottles of beer.
Take one down and pass it around, 13 bottles of beer on the wall.

13 bottles of beer on the wall, 13 bottles of beer.
Take one down and pass it around, 12 bottles of beer on the wall.

12 bottles of beer on the wall, 12 bottles of beer.
Take one down and pass it around, 11 bottles of beer on the wall.

11 bottles of beer on the wall, 11 bottles of beer.
Take one down and pass it around, 10 bottles of beer on the wall.

10 bottles of beer on the wall, 10 bottles of beer.
Take one down and pass it around, 9 bottles of beer on the wall.

9 bottles of beer on the wall, 9 bottles of beer.
Take one down and pass it around, 8 bottles of beer on the wall.

8 bottles of beer on the wall, 8 bottles of beer.
Take one down and pass it around, 7 bottles of beer on the wall.

7 bottles of beer on the wall, 7 bottles of beer.
Take one down and pass it around, 6 bottles of beer on the wall.

6 bottles of beer on the wall, 6 bottles of beer.
Take one down and pass it around, 5 bottles of beer on the wall.

5 bottles of beer on the wall, 5 bottles of beer.
Take one down and pass it around, 4 bottles of beer on the wall.

4 bottles of beer on the wall, 4 bottles of beer.
Take one down and pass it around, 3 bottles of beer on the wall.

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer. 
Go to the store and buy some more, 99 bottles of beer on the wall.`.split('\n').filter(Boolean)

Or, for this particular "song", which is very repetitive, you can create the lyrics yourself, which might have been the point of the exercise, though it's not very clear:

const sing = () => [
  ...Array.from(
    { length: 98 },
    (_, i) => [
      `${99 - i} bottles of beer on the wall, ${99 - i} bottles of beer.`,
      `Take one down and pass it around, ${99 - i - 1} bottles of beer on the wall.`
    ]
  ).flat(),
  `1 bottle of beer on the wall, 1 bottle of beer.`,
  `Take one down and pass it around, no more bottles of beer on the wall.`,
  `No more bottles of beer on the wall, no more bottles of beer.`,
  `Go to the store and buy some more, 99 bottles of beer on the wall.`
];
const song = sing();
console.log(song[0] === '99 bottles of beer on the wall, 99 bottles of beer.');

这篇关于根据字符串中的标点将字符串拆分为数组元素-JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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