如何创建多维数组,该数组的维数由字符串的长度确定,以创建数组 [英] How do I create a Multidimensional Array which the dimension is determined by the length of the strings to create the array
问题描述
我想从HTML中的input元素得到的给定字符串创建矩形多维数组。矩形的尺寸取决于字符串的长度。
I want to create a rectangular multidimensional array from a giving string gotten by input element in HTML. The dimensional of the rectangular will be determined by length of the string.
例如
ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots
纯文本应组织为矩形。矩形(rxc)
的大小应取决于消息的长度,以使 c> = r
和 c-r< = 1
,
其中 c 是列数和r是行数。
我们的规范化文本长54个字符,规定了c = 8和r = 7的矩形:
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
The plain text should be organized in to a rectangle. The size of the rectangle (r x c)
should be decided by the length of the message, such that c >= r
and c - r <= 1
,
where c is the number of columns and r is the number of rows.
Our normalized text is 54 characters long, dictating a rectangle with c = 8 and r = 7:
"ifmanwas"
"meanttos"
"tayonthe"
"groundgo"
"dwouldha"
"vegivenu"
"sroots
请检查 https://codepen.io/djtush/pen/ewvNEG 看看我做了什么。
Kindly check https://codepen.io/djtush/pen/ewvNEG to see what I have done.
const myFunction = () =>
{
let message = document.getElementById("message").value;
if (message.length < 50) {
alert ("Your Message can not be less than 50 Characters");
}
else {
let removedSpace = message.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
document.getElementById("noSpace").innerHTML = removedSpace;
let squared = Math.sqrt(removedSpace.length);
let column = Math.ceil(squared);
let row = Math.floor(squared);
document.getElementById("no-Space").innerHTML = row;
}
}
我希望有一段array
I expect to have a block of array
推荐答案
让正则表达式执行此工作:
Let the regex do the job:
s = 'ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots'
n = Math.ceil(Math.sqrt(s.length))
r = s.match(new RegExp(`.{1,${n}}`, 'g'))
console.log(r)
这篇关于如何创建多维数组,该数组的维数由字符串的长度确定,以创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!