Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Undefined JavaScript error
    var broked = broken[i].charAt(0).toUpperCase() + broken[i].substring(1); Why would this result in an error telling me that broken is undefined? It works fine when I don't use i within a for loop as the index.
    weweboom posted this question on 3/25/14 | javascript
    Answers
  • +
  • 1
  • -
  • You need to include more code here. The context is important. There is no way for us to tell if you're doing something silly with "broken" before this line or not.
  • +
  • 0
  • -
  • I believe this is coderbyte editor specific. Even I faced the same challenge.
  • +
  • 0
  • -
  • Do you have out of boundary error? I mean if the value of the index is too big or small, you could get undefined value.
  • +
  • 0
  • -
  • HI weweboom, I assume "broken" to be an array you loop over in a for/while loop? Then you 're probably trying to access an item of broken via the "i"-index and the value of "i" is greater than the length of broken. Example: var broken = ["a","b","c"]; broken.length // 3 broken[0] = =="a"; broken[1] = =="b"; broken[2] = =="c"; broken[3] === undefined I suspect your for loop to be something like this: for(var i = 0; i <= broken.length;i++) { }
  • +
  • 0
  • -
  • here is my solution. It works perfect on my pc and editor yet it keeps giving me an error. If I dont use console.log (str) ate end of the function it gives me undefined.Can anyone please help me,thanks.
    function LetterChanges(str) { 
      var str = str.toLowerCase();
    
      if(str.match(/[^a-zA-Z]/g)){
        var oddCharacterToCheck = str.match(/[^a-zA-Z]/g);
        var oddCharacterLength = oddCharacterToCheck.length;
      
        for(var i = 0;i < str.length;i++){
          for(var j = 0;j < oddCharacterLength;j++){
            if(str[i] == oddCharacterToCheck[j]){
              str = str.replace(str[i],String.fromCharCode(str[i].charCodeAt() - 1)); 
            }
          }
        }
     
       doYourThingWithString(str);
    
    
      }  else { 
        doYourThingWithString(str);
      }
    }
    
    function doYourThingWithString(str){
      var str = str;
      var vowels = ['a','e','i','o','u'];
      var i;
    
      var strLength = str.length;
      var charCodeArr = [];
      var charLetterArr = [];
    
      //determines the characters code value then adds 1 to the value and pushes it into array
      //does one check to see if the character is a z value then has to store the character code value for letter a
    
      for(i = 0;i < strLength;i++){
        if(str.charCodeAt(i) == 122){
          charCodeArr.push(97);
        }   else {
          charCodeArr.push((str.charCodeAt(i)) + 1);
        }  
      }
     
      //uses the character value to determine the character then push it into the charLetterArr array
      for(i = 0;i < strLength;i++){ 
        charLetterArr.push(String.fromCharCode(charCodeArr[i]));  
      } 
      
      
      //joins the array values back to a string
      str = charLetterArr.join('');
      
      //loops throught the new string and if it finds a vowel it makes it uppercase
      for(i = 0;i < vowels.length;i++){
        for(var j = 0;j < str.length;j++){
          if(vowels[i] == str[j]){
            str = str.replace(str[j],vowels[i].toUpperCase());  
          } 
        }   
       }
       console.log(str);
       return str;
    }
    // keep this function call here 
    LetterChanges('fun times!'); 
    
    Log in to write an answer.