2

I want to fill two arrays (sentences and links ) with odd and even indexes of sentence array :

Here is what I tried with no success:

     let link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day"; // 
                 
        let sentence =  link_sentence.split(">")
        let sentences= []
        let links = []
        for(let i = 0; i < sentence.length; i += 2) {  
        
            sentences.push(sentence[i]);
        }
        console.log(sentences)

The expected output should be :

//let links = ["60-1", "6-2", "16-32"];
//let sentences = ["don't you worry", "I gonna make sure that", "tommorow is  another great day"];
2
  • 1
    sentences looks awfully close to being correct, and you haven't even tried to compute links. Commented Aug 6, 2019 at 19:05
  • I have no idea of how links should be computed and yes if sentences don't look awfully close to being correct I would never ask for help Commented Aug 6, 2019 at 19:07

5 Answers 5

4

You could match the parts and omit empty string with splitting.

var link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day",
    sentences = [],
    links = [];

link_sentence
    .match(/\>[^>]+/g)
    .reduce(
        (r, s, i) => (r[i % 2].push(s.slice(1)), r),
        [links, sentences]
    );

console.log(sentences);
console.log(links);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Lets give people a headache :) But nicely done as always, Nina ^_^
2

Your initial attempt is close and if you modify your for loop slightly you can achieve the result you want.

// remove first value from the array if the value is empty
if (!sentence[0]) {
    sentence.shift();
}

for(let i = 0; i < sentence.length; i++) {  
    if (i % 2 === 0) {
        links.push(sentence[i]);
    } else {
        sentences.push(sentence[i]);
    }
}

2 Comments

except that the sentence starts with a > therefor the first link would be empty, then a link, then a sentence, so your index would be incorrect :)
I think it's fine, as it seems that the OP understands the logic behind it :) The only downside might be that the links & sentences would be "disconnected"
2

Here's a simple solution using Array.prototype.reduce:

const sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day".split(">")

const {links, sentences} = sentence.reduce((acc, val, index) => {
  acc[index % 2 === 0 ? 'links' : 'sentences'].push(val);
  return acc;
}, {
  links: [],
  sentences: []
});

console.log(links, sentences);

Comments

1

You can do it like this:

const link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day"
const sentence =  link_sentence.split(">")
const sentences = []
const links = []

sentence.forEach((el, idx) => {
  if (idx % 2 === 0) {
    // if even
    sentences.push(el)
  } else {
    // if odd
    links.push(el)
  }
})

Comments

1

You could use:

let link_sentence = ">60-1> don't you worry >6-2> I gonna make sure that >16-32> tomorrow is another great day"; // 
                 
let sentence =  link_sentence.split(">")
let sentences = []
let links = []

// removing the first element of array: [""]
sentence.splice(0, 1)

// iterating sentence
sentence.forEach((item, index) => {
  // if the index is even push to links, else push to sentences
  if (index % 2 === 0) {
    links.push(item)
  } else {
    // trim white spaces
    sentences.push(item.trim())
  }
})
console.log(links)
console.log(sentences)

Comments

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.