Spinal-Tap-Case problem

Spinal-Tap-Case problem
0

#1

Tell us what’s happening:

i am getting an error message saying"you have a potential infinite loop at line 5".
however i dont see anything in my code that can cause an infinite loop…please help me out.

function spinalCase(str) {

for(var i=0;i<str.length;i++)
{
// nonprotect
var x=str.charAt(i);
var z="-"+x;
z=z.trim();
var y=str.charCodeAt(i);
if(y>=65&&y<=90)//ASCII range for capital alphabets
{
str=str.replace(x,z);
}
if(x==" “)
{
str=str.replace(x,”-");
}
if(x=="_")
{
str=str.replace(x,"-");
}

}
str=str.toLowerCase();
console.log(str);

}//end of function
spinalCase(‘AllThe-small Things’);

Your code so far

function spinalCase(str) {
 
for(var i=0;i<str.length;i++)
  {
    // nonprotect
    var x=str.charAt(i);
    var z="-"+x;
    z=z.trim();
    var y=str.charCodeAt(i);
if(y>=65&&y<=90)//ASCII range for capital alphabets
  {
    str=str.replace(x,z);
  }
    if(x==" ")
      {
        str=str.replace(x,"-");
      }
    if(x=="_")
      {
           str=str.replace(x,"-");
       }
  
  }
  str=str.toLowerCase();
  console.log(str);



}//end of function
spinalCase('AllThe-small Things');

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

You do in fact have an infinite loop. If you add the line //noprotect as the first line in your code, you’ll crash your browser. This is happening because you are modifying the string as you iterate over it. Every time that you hit a capital letter, you are adding an infinite number of -s.

Example:
str is "Apple"
i is 0
x is "A"
z is "-A"
You replace x with z and str is now "-Apple"
i is 1
x is "A" (because it’s now the second letter)
lather rinse repeat


#3

the example you gave made me understand the error…thank u so much for the help…