1

I have given string "asdlfjsahdkljahskl" and have given array [1,2,3,1,7,2,1,2].My final output should be a, sd, lfj, s, ahdklja, hs, kl.

I know how to split the string but I don't know how to compare the string and cut according to given array

HTML Code

<button onclick="myFunction()">click me</button>
<p id="demo"></p>

JavaScript

var width = [1,4,2,6,1,1,10,1];

function myFunction() {
    var str = "abcdefghijklmnopqrstuvwxyz";
    var res = str.split("");

    for(var j=0; j.length;j++){
        document.write(width[j]);
    }
    document.getElementById("demo").innerHTML = res;
 }

Thank you for your help

3
  • j.length does not make any sense since it is a number. Commented Jan 23, 2017 at 16:56
  • you can keep the loop. You will need array for result and local variable keeping rest of the string after sub Commented Jan 23, 2017 at 16:56
  • Your expected output is probably wrong... Commented Jan 23, 2017 at 17:03

3 Answers 3

5

You could slice the string with a stored value and a new length.

var string = "asdlfjsahdkljahskl",
    array = [1, 2, 3, 1, 7, 2, 1, 2],
    p = 0,
    result = array.map(function (a) {
        return string.slice(p, p += a);
    });

console.log(result);

The same with a for loop

var string = "asdlfjsahdkljahskl",
    array = [1, 2, 3, 1, 7, 2, 1, 2],
    p = 0,
    i = 0,
    result = []


for (i = 0; i < array.length; i++) {
    result.push(string.slice(p, p + array[i]));
    p += array[i];
}

console.log(result);

Sign up to request clarification or add additional context in comments.

3 Comments

how would this be possible without .map
@danny you can use any iterator, honestly (assuming you use them properly) - my answer uses .forEach() if you are interested in that one
I am beginner, so I am looking very simple logic not using for each or .map. Thanks for helping
0

var string="asdlfjsahdkljahsxkl",
    array=[1,2,3,1,7,2,1,2];
function myFunction() {
array.forEach(function(index){
  var yourvalue=string.slice(0,index);
  console.log(yourvalue);
  document.getElementById("demo").innerHTML += yourvalue+'\n';
  string=string.slice(index,string.length);
});
  }
<button onclick="myFunction()">click me</button>
<p id="demo"></p>

Comments

0

An alternative to Nina's answer is to convert your string to an array, like you have done, and use the .splice() function like so:

var str = "asdlfjsahdkljahskl",
  arr = [1, 2, 3, 1, 7, 2, 1, 2];

function extractValues (str, strLens) {
  str = str.split("");
  strLens.forEach(function (strLen) {
    console.log(str.splice(0, strLen).join(""));
  });
}
extractValues(str, arr);

2 Comments

what does .foreach do ? does it compare every array element. I am running on my local editor is giving me error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.