I'm trying to solve a problem I have that requires arrays and loops.
The task is just to take a random string and write out all the letters so that it looks like this: R, Ra, Ran, Rand ,Rando , Random, RandomS, RandomSt, RandomStr, RandomStri, RandomStrin, RandomString.
I solved that part like this:
function sometext() {
var temptext = document.getElementById("textinput").value;
var temparray = [];
temparray = temptext.slice();
for (var i = 0; i <= temparray.length; i ) {
document.getElementById("printit").innerHTML = temparray.slice(0, i) "<br/>";
}
}
<input id="textinput" onchange="sometext()">
<div id="printit"></div>
The code runs that string that it gets from an inputbox that anyone writes in.
The next step is to have this string remove the first letter and then write it out like above so it would look like this: a, an, and, ando, andom, andomS, andomSt, andomStr, andomStri, andomStrin, andomString and then remove the first letter again so the output would be: n, nd, ndo, ndom, ndomS, ndomSt, ndomStr, ndomStri, ndomStrin, ndomString and so on until it runs out of letters to remove from. I don't know how to make it work for any amount of letters.
Example output
R
Ra
Ran
Rand
Rando
Random
a
an
and
ando
andom
n
nd
ndo
ndom
d
do
dom
o
om
m
CodePudding user response:
How about a map and shift
const output = document.getElementById("printit");
function sometext(fld) {
let temptext = [...fld.value]; // or fld.value.split("")
const arr = []
while (temptext.length > 0) {
arr.push(
temptext.map((_, i) => temptext.slice(0, i 1).join("")).join("<br>")
)
temptext.shift(); // drop first letter
}
output.innerHTML = arr.join("<br/>")
}
<input id="textinput" oninput="sometext(this)">
<div id="printit"></div>
CodePudding user response:
When you get array with all needed values, just iterate through and print it as you whant or to html or to console
const text = 'Random';
const result = [...text].flatMap((ch, index) => {
const righPart = text.slice(index, text.length);
const leftParts = [...righPart].map((ch, index) => righPart.slice(0, index 1))
return leftParts;
});
console.log(result)
.as-console-wrapper{min-height: 100%!important; top: 0}
--- update ---
It just combination of two similar functions
First return array with only right parts of string:
const text = 'Random'
[...text].map((ch, index) => text.slice(index, text.length))
// ['Random', 'andom', 'ndom', 'dom', 'om', 'm']
Second is similar and retern only left parts:
const text = 'Random'
[...text].map((ch, index) => text.slice(0, index 1))
// ['R', 'Ra', 'Ran', 'Rand', 'Rando', 'Random']
In solution I use .flatMap()
to make result array flatten. That's all.
CodePudding user response:
<script>
function sometext() {
var temptext = document.getElementById("textinput").value;
var temparray = [];
temparray = temptext.slice();
for (var i = 0; i <= temparray.length; i )
{
document.getElementById("printout").innerHTML = temparray.slice(0, i) "<br/>";
if (i == temparray.length)
{
for (var i = 1; i <= temparray.length; i )
{
document.getElementById("printout").innerHTML = temparray.slice(1, i) "<br/>";
if (i == temparray.length)
{
for (var i = 2; i <= temparray.length; i )
{
document.getElementById("printout").innerHTML = temparray.slice(2, i) "<br/>";
if (i == temparray.length)
{
for (var i = 3; i <= temparray.length; i )
document.getElementById("printout").innerHTML = temparray.slice(3, i) "<br/>";
if (i == temparray.length)
{
for (var i = 4; i <= temparray.length; i )
{
document.getElementById("printout").innerHTML = temparray.slice(4, i) "<br/>";
if (i == temparray.length)
{
for (var i = 5; i <= temparray.length; i )
{
document.getElementById("printout").innerHTML = temparray.slice(5, i) "<br/>";
if (i == temparray.length)
{
for (var i = 6; i <= temparray.length; i )
{
document.getElementById("printout").innerHTML = temparray.slice(6, i) "<br/>";
}
}
}
}
}
}
}
}
}
}
}
}
}
</script>
<p><input type="text" id="textinput" value=""></p>
<p><input type="button" value="Check it out" onclick="sometext()">
</p>
<div id="printout"></div>
I'm showing how I first answered it, it runs a loop and prints out the first iteration, then when i == temparray.length it starts on a new loop with i starting on the next number and prints out the next iteration and when it reaches the length of the array it starts again with the next number and this process continues for 6 iterations. Now this doesn't look very nice since it's not a generalized solution, and that's what I'm trying to figure out but with no success. As the code is now, it does it for 6 character words, in order to make it for 7 characters I just need to add another if (i == temparray.length) and for (var i = [previousNumber 1]; i< temparray.length; i ) and then write it out on the div element. It works but it isn't very pretty in my opinion.