Home > database >  How to make everyother word in string upper
How to make everyother word in string upper

Time:10-18

I don't understand why when I return element.toUpperCase() it doesn't return it back to the array as uppercase, however if I console.log(element.toUpperCase()) before that return statement it displays as upper case

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => {
  string = string.split(" ");
  oddWords = string.filter((element, index) => {
    if (index % 2 === 0) {
      return element;
    }
    return element.toUpperCase();
  });
  console.log(oddWords.join(" "));
};

uppercaseOddWords(sentence);

CodePudding user response:

filter callback expected to return truthy or falsy value, in your case all values with be truthy and will not filter anything, what you are looking for is map method.

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => {
  string = string.split(" ");
  oddWords = string.map((element, index) => {
    if (index % 2 === 0) {
      return element;
    }
    return element.toUpperCase();
  });
  console.log(oddWords.join(" "));
};

uppercaseOddWords(sentence);

CodePudding user response:

.filter() should only be used if you want to pick elements of the array and leave out something based on a boolean logic test. The function expects a logical condition, and can either return true or false. You are using the function wrong.

.map() on the other hand is used to transform the elements in the array from their original state to any other state based on the logic in the function.

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => {
  const words = string.split(" ");
  const oddWords = words.map((word, index) => {
    if (index % 2 === 0) {
      return word;
    }
    return word.toUpperCase();
  });
  console.log(oddWords.join(" "));
};

uppercaseOddWords(sentence);

CodePudding user response:

Still wondering why returning it doesn't work, but I got the function to work this way;

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => {
  let newString = "";
  string = string.split(" ");
  string.filter((element, index) => {
    if (index % 2 === 0) {
      return (newString  = element   " ");
    }
    return (newString  = element.toUpperCase()   " ");
  });
  console.log(newString.trim()); //remove trailing spaces
};

uppercaseOddWords(sentence);

CodePudding user response:

You can achieve this by using Array.map() method which will return same number of elements as passed, Based on the condition for odd elements, We can convert odd elements in an upper case.

Live Demo :

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => {
  string = string.split(" ");
  const res = string.map((element, index) => (index % 2 === 0) ? element.toUpperCase() : element);
  console.log(res.join(" "));
};

uppercaseOddWords(sentence);

CodePudding user response:

Based on @Mina's answer,I change it to one-liner

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => string.split(" ").map((d,i) => i%2==0 ? d: d.toUpperCase()).join(" ");

console.log(uppercaseOddWords(sentence));


Update:If you want to do it using filter(),you need to return all the elements and change the element dynamiclly

const sentence = "fur pillows are hard to actually sleep on";

const uppercaseOddWords = (string) => {
  let data= string.split(" ");
  data.filter((element, index) => {
    if (index % 2 != 0) {
      data[index] = element.toUpperCase() // we can use index to change the element to uppercase
    }
    return true; // return all the elements
  });
  data = data.join(" ")
  console.log(data);
  return data
};

uppercaseOddWords(sentence);

  • Related