Intermediate Algorithm Scripting - Convert HTML Entities

Tell us what’s happening:
why does case statement work here but not if-else ?
I have even used break to stop the conversion of ‘"’ to ‘&quot;’ …suprisingly only ‘&quot’ is having issues and not ‘<’ or ‘>’ or anthing else.

Your code so far

function convertHTML(str) {
  let a=str.split('')
  //console.log(a)
  for (let i=0;i<a.length;i++){
    if (a[i]=='&'){
      a[i]='&amp;'
      break;

    }
    else if(a[i]=='>'){
      a[i]='&gt;'
      break;
    }
    else if(a[i]=='<'){
      a[i]='&lt;'
      break;
    }
    else if(a[i]=='"'){
      a[i]='&quot;'
      break;
    }
    else if(a[i]=='<>'){
      a[i]='&lt;&gt;'
      break;
    }
    else if(a[i]=="'"){
      a[i]='&apos;'
      break;

    }
  


  }
  console.log(a)

  return a.join('');
}

//console.log(convertHTML("Dolce & Gabbana"));
console.log(convertHTML(convertHTML('Stuff in "quotation marks"')))

Challenge: Intermediate Algorithm Scripting - Convert HTML Entities

Link to the challenge:

It is the break commands which are breaking your code actually.
If you break out of the for loop when you modify an array element, it means that no subsequent array elements will be checked.
If I simply remove all of your break commands, your code passes.
Still, I’d use a switch statement anyway, as it’s far more elegant.

1 Like

very strange , I hadn’t been using break before and it wasn’t passing …now after removing break it does…I don’t know what I did.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.