Convert HTML Entities ~ Int. Algo

Tell us what’s happening:

A little feedback would be much appreciated!

Your code so far


function convertHTML(str) {
// :)
let charArr = str.split('');
let charMatch;
for (var i of charArr){
  console.log(check(charArr[i]))
  charMatch = check(charArr[i]);
  if (charMatch !== false){
    charArr.splice(i,1,charMatch);
  }
}
 function check () {
   let char = arguments;
  switch (char) {
   case '&': 
      char = '&';
   break;
   case '<': 
     char = '&lt;';
     break;
   case '>': 
     char = '&gt;';
     break;	
   case '"': 
     char = '&quot;';
     break;
     default:
    return false;
  }
  return char;
}

console.log(charArr.join(''))
return str;
}

convertHTML("Hamburgers < Pizza < Tacos");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

Challenge: Convert HTML Entities

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities

What problems are you having? What do the failing tests say? What else have you tried? Do you have any idea where the problem may be?

The line above is not doing what you think it is doing. arguments is an array-like object containing property/value pairs with the property names such as “0”, “1”, etc… (like indices of an array) and values of the corresponding properties.

The majority of your check function’s logic assumes a single character is passed. Why not just pass a single character (char would become the function’s parameter) to it instead?

Do you understand what the value of i is in each iteration of the for loop? If no, I suggest adding a simple console.log(i) to find out. I think you will be surprised.

Two more things:

  1. Currently, you are returning the original string passed to the function. Is that what you want to do?
    2.You are also missing a conversion specified in the instructions.

Thanks for your reply! I was in a rush when posting this and didn’t leave any details. I did solve it in the end though! :slight_smile:

Thanks Randell!

I figured out what the for … in was doing. I then changed to a regular for loop, added the missing conversion, and then joined the array into a string and voilà it passed :slight_smile:

function convertHTML(str) {
  // &colon;&rpar;
  let charArr = str.split('');
  let charMatch;
  for (var i=0;i<charArr.length;i++){
    charMatch = check(charArr[i]);
    if (charMatch !== false){
      charArr.splice(i,1,charMatch);
    }
  }
  function check (char){
  switch (char) {
    case '&': 
      return '&amp;';
      break;
    case '<': 
      return '&lt;';
      break;
    case '>': 
      return '&gt;';
      break;	
    case '"': 
      return '&quot;';
      break;
      case "'": 
      return '&apos;';
      break;
    default:
      return false;
    }
  }
  
str = charArr.join('');
console.log(str)
return str;
}
convertHTML("Schindler's List"); ```

You were actually using a for … of loop before. You could use a for … in loop instead of the regular for loop if you wanted.