如何在单词的特定索引之间洗牌中间字母?
How to shuffle middle letters in between specific indexes of a word?
这是我目前所掌握的
mounted() {
const randomVariants = [...Array(3)].map(() =>
this.baseWord
.split('')
.sort(() => 0.5 - Math.random())
.join('')
)
const variantsWithoutInitialWord = randomVariants.filter(
(word) => word !== this.baseWord // !== this.baseWord , it removes if baseWord present
)
this.result = [...new Set(variantsWithoutInitialWord)] // removing duplicates
},
我需要一个字符串值的多个值。喜欢“橙色”,并生成多个喜欢:
- “华丽”
- “俄勒冈州”
- “奥格兰”
前两个字符和最后一个字符相同。它只会改变其他字符,我还需要一个原始值。 lik "xy.........z" 只有x
, y
和 z
是静态的,而其他内部字符将随机打乱。
这应该很好用
<template>
<div>
<p>
The idea is to have a string, with it's first 2 indexes and last one
untouched while all the others indexes are shuffled
</p>
<pre>combination found for string '{{ baseWord }}'</pre>
<pre>actual list: {{ results }}</pre>
</div>
</template>
<script>
const shuffledMiddleLetters = (array) =>
array
.split('')
.sort(() => 0.5 - Math.random())
.join('')
const wordMiddlePart = (word) => word.slice(2, word.length - 1)
export default {
data() {
return {
baseWord: 'watermelon',
results: [],
}
},
mounted() {
const shuffledMiddleLettersVariants = [...Array(50)].map(() =>
shuffledMiddleLetters(wordMiddlePart(this.baseWord))
)
const dedupedVariants = [
...new Set([
wordMiddlePart(this.baseWord),
...shuffledMiddleLettersVariants,
]),
]
this.results = dedupedVariants.map((dedupedVariants) =>
[
this.baseWord.slice(0, 2),
dedupedVariants,
this.baseWord[this.baseWord.length - 1],
].join('')
)
},
}
</script>
这是它的样子
这是我目前所掌握的
mounted() {
const randomVariants = [...Array(3)].map(() =>
this.baseWord
.split('')
.sort(() => 0.5 - Math.random())
.join('')
)
const variantsWithoutInitialWord = randomVariants.filter(
(word) => word !== this.baseWord // !== this.baseWord , it removes if baseWord present
)
this.result = [...new Set(variantsWithoutInitialWord)] // removing duplicates
},
我需要一个字符串值的多个值。喜欢“橙色”,并生成多个喜欢:
- “华丽”
- “俄勒冈州”
- “奥格兰”
前两个字符和最后一个字符相同。它只会改变其他字符,我还需要一个原始值。 lik "xy.........z" 只有x
, y
和 z
是静态的,而其他内部字符将随机打乱。
这应该很好用
<template>
<div>
<p>
The idea is to have a string, with it's first 2 indexes and last one
untouched while all the others indexes are shuffled
</p>
<pre>combination found for string '{{ baseWord }}'</pre>
<pre>actual list: {{ results }}</pre>
</div>
</template>
<script>
const shuffledMiddleLetters = (array) =>
array
.split('')
.sort(() => 0.5 - Math.random())
.join('')
const wordMiddlePart = (word) => word.slice(2, word.length - 1)
export default {
data() {
return {
baseWord: 'watermelon',
results: [],
}
},
mounted() {
const shuffledMiddleLettersVariants = [...Array(50)].map(() =>
shuffledMiddleLetters(wordMiddlePart(this.baseWord))
)
const dedupedVariants = [
...new Set([
wordMiddlePart(this.baseWord),
...shuffledMiddleLettersVariants,
]),
]
this.results = dedupedVariants.map((dedupedVariants) =>
[
this.baseWord.slice(0, 2),
dedupedVariants,
this.baseWord[this.baseWord.length - 1],
].join('')
)
},
}
</script>
这是它的样子