gpt4 book ai didi

javascript - JS中的拆分数组

转载 作者:行者123 更新时间:2023-12-04 09:55:10 24 4
gpt4 key购买 nike

我正在制作一个纯 js 刽子手游戏。我有一个不同单词的数组。我试图将数组拆分为单个字母,但每个字母都得到“,”。

例如:在数组 [station] 中,当我拆分它返回“s,t,a,t,i,o,n”。

所以我想用“,”把它分开。

这是代码:

// Word we want to use to the hangman game
let words = ["definition", "programming", "relation", "station","gamification","playstation"];

//This chooses random word from words array and * with words length to not go over the array length
let random = Math.floor(Math.random(words) * words.length);

//This will split up the word ex: station to [s,t,a,t,i,o,n]
let wordSplit = words[random].split('');


let wordLetters = document.getElementById("letters")

wordLetters.innerHTML = wordSplit;

function wordInput(){
let lettersDiv = document.getElementById("letters");
let html = "<p id = 'letters'>" + wordSplit + "</p>";
return html;
}

how it displays

最佳答案

你可以尝试更换

let wordSplit = words[random].split('');


let wordSplit = words[random].split('').join(" ");  // there is a space in join 

关于javascript - JS中的拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61935924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com