0

I am a little bit old school javascript coder I have this code which changes the word with thias sign [yellow] for example:

same ======> s[yellow]m

name ======> n[yellow]m

my question is how to change this code into modern javascript using ternary, map functions.

function yellowMaker(word) {
  var newWord = "";
  for (var i = 0; i < word.length; i++) {
    if (word[i] === "a" && word[i+2] === "e") {
      newWord += "[yellow]";
    } else if (word[i-2] === "a" && word[i] === "e") {
      newWord += "";
    } else {
      newWord += word[i];
    }
  }
  return newWord;
}
2
  • 3
    const yellowMaker = word => word.replace(/a(.)e/g, "[yellow]$1"); -- doesn't need to be excessively "modern", just use a basic regex instead of manually parsing the string. Commented Dec 14, 2020 at 16:42
  • @Niet the Dark Absol This should be an answer (I'd upvote it)! Commented Dec 14, 2020 at 16:59

3 Answers 3

1

You can do the following using ternary and map function,

const yellowMaker = (word) => {
  return Array.from(word).map((char, index, arr) => {
    const newChar = char === 'a' && arr[index+2] === 'e' ? '[yellow]' : char;
    arr[index+2] = newChar !== char ? '' : arr[index+2];
    return newChar;
  }).join('');
}

console.log(yellowMaker('same'));
console.log(yellowMaker('name'));

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

Comments

1

A simple search and replace with a regex would also do the job here.

function yellowMaker(word) {
  return word.replace(/a(.)e/gs, "[yellow]$1");
}

console.log(yellowMaker("same"));
console.log(yellowMaker("name"));
console.log(yellowMaker("aaee"));
console.log(yellowMaker("abcadefg"));

The only scenario this does not cover is "aaee" which should result in "[yellow]a[yellow]e". However the above solution produces "[yellow]ae" If you do want to cover this scenario you can still use a regular expression, but your current solution might be cleaner.

function yellowMaker(word) {
  const regex = /a(.)e/gs;
  let match, index = 0, result = "";
  while (match = regex.exec(word)) {
    result += word.slice(index, match.index) + "[yellow]" + match[1];
    index = regex.lastIndex;
    regex.lastIndex -= 2;
  }
  return result + word.slice(index, word.length);
}

console.log(yellowMaker("same"));
console.log(yellowMaker("name"));
console.log(yellowMaker("aaee"));
console.log(yellowMaker("abcadefg"));

The regex s flag is relatively new in JavaScript. If you need something that is more compatible, replace the . with [^] (see: Regular expression syntax cheatsheet) but a lot of people also use [\s\S]. After replacing the . you can drop the s flag.

. without the s flag does not match newline characters. Whereas your current code does match the string "a\ne". If you don't care about newline characters you can leave the . and omit the s flag.

Comments

0
class A {
  constructor() {
    this.fname = "Rahul";
  }
}
class B extends A {
  constructor() {
    super();
    this.lname = "mankar";
  }
}

class C extends B {
  constructor() {
    super();
    this.name = this.fname + " " + this.lname;
  }
  getFullName() {
    return this.name;
  }
}
const obj = new C();
console.log(obj.getFullName());

https://codesandbox.io/s/bold-architecture-thlryz?file=/index.js:0-345

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.