-1
> let fullInteger = '';

const sentenceText = 'junayed';

for (let i = 0; i < sentenceText.length; i++) {
    if (sentenceText[i] == 'n' & sentenceText[i + 1] == 'a') {
        let newChar = sentenceText[i]
        newChar = 'a';
        fullInteger = fullInteger + newChar;
      
    }
    else if (sentenceText[i] == 'a' & sentenceText[i - 1] == 'n') {
        let newChar = sentenceText[i]
        newChar = 'n';
        fullInteger = fullInteger + newChar;
        
    }
    else{
        fullInteger = fullInteger + sentenceText[i];
    }
}
console.log(fullInteger);

It will take a string and change it if it finds "na" to "an". Does any of you know how can I shorten it, or any alternative idea?

Llama
  • 25,925
  • 5
  • 49
  • 68
  • 2
    so, you want to change all occurrences of `na` to `an`? `const fullInteger = sentenceText.split('na').join('an');` – Jaromanda X Sep 21 '20 at 03:39
  • If you could explain what you are trying to achieve, we could help in a better way – Rajesh Sep 21 '20 at 03:40
  • Please pay attention to the rules and guidelines of this site. Specifically [Tagging](https://stackoverflow.com/help/tagging): _"The only time you should use tags in your title is when they are organic to the conversational tone of the title."_ Adding #javascript to the end of your question title is not appropriate. – Llama Sep 21 '20 at 03:40
  • 1
    You can also look into regex: `const newStr = sentanceText.replace(/na/gi, 'an ')`. Note: the flags passed: `gi` have their meaning. `i` means case insensitivity. So `'na'` and `'NA'` both will be captured. `g` means global. So it will check for entire string for passed pattern – Rajesh Sep 21 '20 at 03:41
  • 2
    FYI `&` is not the same as `&&`. .......... `&` is bitwise. `&&` is the traditional as in boolean AND – GetSet Sep 21 '20 at 03:41
  • @Rajesh - are you sure about the `i`? look at the code :p – Jaromanda X Sep 21 '20 at 03:42
  • Usually when you work with pattern, a following requirement is case-insensitivity. Hence I added `i`. I'll add explanation – Rajesh Sep 21 '20 at 03:43
  • Huh? look at the code in the question @Rajesh not some question somewhere you may have read :p – Jaromanda X Sep 21 '20 at 03:50
  • It is a good attempt at an algorithm I must say (Junayed). Despite its out of bounds logical errors on `i - 1` and `i + 1` – GetSet Sep 21 '20 at 03:50
  • @JaromandaX Point taken. Also, I have added a community answer, so if you wish, you may add you suggestion in it. Not adding mine as it has already been answered – Rajesh Sep 21 '20 at 04:14

2 Answers2

1

this should be a simple replace

const sentenceText = 'junayed';
const sentenceText1 = 'junyed';

function replaceContent(sentence){
  return sentence.replace(/na/g,'an');
}


console.log(replaceContent(sentenceText));// gives "juanyed" - ie: 'na' has been replaced by 'an'

console.log(replaceContent(sentenceText1));// gives junyed - ie: no 'na' to be replaced
gavgrif
  • 13,077
  • 2
  • 17
  • 22
1

There are many ways you can achieve this. Lets first try to use your algorithm:

Improvements:

  • Suggestions by GetSet,
    • && and not &: & means bitwise AND and double && means the logical AND.
    • Error on i-1 and i+1: If a string starts with a or ends with n, your code will break. You should add checks for them. You can so that as:
!!sentanceText[index]

Other suggestions:

  • You do not need multiple assignments. Your second assignment makes first one void.
let newChar = sentenceText[i]
newChar = 'a';
  • You so not need else if case. Since you have already checked for [i] === 'n' && [i+1] === 'a', you can just set you newChar = 'an' and skip next iteration as it has been take care of. You can do that by incrementing i
if (sentenceText[i] == 'n' && sentenceText[i + 1] == 'a') {
  fullInteger = fullInteger + 'an';
  i++;
}

Following is a sample

function getProcessedStr(sentenceText) {
  let fullInteger = '';
  for (let i = 0; i < sentenceText.length; i++) {
    if (sentenceText[i] === 'n' && !!sentenceText[i + 1] && sentenceText[i + 1] === 'a') {
      fullInteger = fullInteger + 'an';
      i++
    } else {
      fullInteger = fullInteger + sentenceText[i];
    }
  }
  console.log(fullInteger);
  return fullInteger
}

getProcessedStr('junayed');
getProcessedStr('junayedn');

References:

Rajesh
  • 21,405
  • 5
  • 35
  • 66