# Getting unknown error and also would love some pointers because I am stuck at that point

I keep getting this error at line 28( which is the line where I call the main function) , I tried it on replit.com also but did not work.
My logic so far is to have an array of alphabet, then go through the input string one by one and look its index in alphabet. when I get the index if its below 12(letter:M) then add 13 to the iterator and get the letter from alphabet array
else if its higher than 12, meaning it will need to go beyond the last letter and start all over again , then I subtract 13 from the iterator and that difference will get me the letter from alphabet array

``````  **Your code so far**
``````
``````      function rot13(str) {
// console.log(str.split(""))
const strArr = str.toLowerCase().split("")
let indexArr=[]
let char;
const alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
// for each to go over input to get each letter's index in alphabet array
strArr.forEach(letter => {
// console.log(alphabet.indexOf(letter))
return indexArr.push(alphabet.indexOf(letter))
})
console.log(indexArr)
// going over the returned indexArr that has the index for the needed chars
for(let i =0; i < indexArr.length; i++){
if (indexArr[i] <= 12){
console.log(alphabet)
char = char + alphabet[indexArr[i] + 13]

} else if(indexArr[i] > 12) {
char = char + alphabet[indexArr[i] - 13]

}

return char;
}

rot13("SE");
``````
``````
function rot13(str) {
return str;
}

rot13("SERR PBQR PNZC");
``````
``````  **Your browser information:**
``````

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

Challenge: Caesars Cipher

Just copy pasting your code, you have syntax error: unexpected token, means there’s a typo. It said there’s an extra character in the function, but may as well mean there’s a character left out. Probably missing a closing curly brace for the for loop?

not sure if I can spot it, I changed my code quite a lot now. been like 2hrs! I have to leave for work now so I am going to dump in my code on the hopes someone can give some pointers .

``````function rot13(str) {
const strArr = str.toLowerCase().split("")
let indexArr=[]
let char=[]
let diff;
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
strArr.forEach(letter => {
if(alphabet.indexOf(letter) === -1){
indexArr.push(letter)
}

else {    return indexArr.push(alphabet.indexOf(letter))
}
// console.log(letter)
// console.log(alphabet.indexOf(letter))
})

// for(let i=0; i < str.length; i++){
//   // if (alphabet.indexOf(str[i]) !== -1){
//   //   console.log(alphabet[str[i]])
//   // }
//   console.log(alphabet[str[i]])
//   // console.log(alphabet.indexOf(str[i])
// }

for (let i=0; i < indexArr.length; i++){
//  console.log(str[i])
// console.log(alphabet[indexArr[i]+13])
if(indexArr[i] <= 12){
// console.log(alphabet[indexArr[i]+13])
// console.log(alphabet[indexArr[i]])
char.push(alphabet[indexArr[i]+13])
} else if (indexArr[i] > 12){
// char.push(alphabet[indexArr[i]-13])
diff = (indexArr[i] - 13)
char.push(alphabet[diff])

} else if(indexArr[i] === -1) {
// console.log(str[i])
console.log(indexArr[i])
char.push(indexArr[i])
}
// console.log(char)console.log(char)

}
console.log(char.join(''))
// console.log(char)
// for(let i =0; i < indexArr.length; i++){
//   if (indexArr[i] <= 12){
//     console.log(alphabet)
//     char = char + alphabet[indexArr[i] + 13]

//   } else if(indexArr[i] > 12) {
//     char = char + alphabet[indexArr[i] - 13]

// }

return char
}

rot13(" SERR PBQR PNZC");`````````

You’re actually very close, IMO. Why didn’t you put a `console.log(char)` right above the `return char` and ran the test once again and see how close you are?
We got something like this:

``````[ 'f',
'r',
'e',
'e',
undefined,
'c',
'o',
'd',
'e',
undefined,
'c',
'a',
'm',
'p' ]
``````

instead of the expected return: `FREE CODE CAMP`

Try `console.log(indexArr)` right before the for loop. Do you get what you wanted? How is it relevant to the `if(indexArr[i] === -1) {` by the way?

Yeah I saw that , and I had a char.join(""),… can’t remember exactly right now as this was not the main problem .
when it comes to : How is it relevant to the if(indexArr[i] === -1) { by the way?
I guess that’s the last step hopefully, I am looking for a (indexOf() that would return (-1)) because that means it’s not part of the alphabet, it can be white space, exclamation mark,…etc
the issue here is that I am getting undefined

Yes, that’s my question.

The position of undefined matched the position where it’s supposed to be an empty string ’ '.
When you’re pushing into indexArr, what did you push into indexArr if you met an empty string ’ '? Were you pushing a -1? Because you’re looking for a -1 in the indexArr when you do the for loop.
You did the .join somewhere in the function, correct, but you did not return the result of the .join.

You are really close.

I started typing that reply like an hour ago and I keep going back to tweek my code I changed my code a little to make sure I capture the non word characters by removing the return from the forEach function and honestly not sure why this is making it capture non word characters now
I passed the first three tests but I am failing the last one , the console.log of char is returning simply a dot at this point
here is my code

``````function rot13(str) {
const strArr = str.toLowerCase().split("")
let indexArr=[]
let char=[]
let diff;
let spaces=[]
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
strArr.forEach(letter => {
if(alphabet.indexOf(letter) === -1){
indexArr.push(letter)
}

else {  indexArr.push(alphabet.indexOf(letter))
}
})

for (let i=0; i < indexArr.length; i++){

if(indexArr[i] == " " ){
console.log(indexArr[i])
char.push(indexArr[i])
// console.log(char)
// console.log(indexArr[i])
}
// if(alphabet.indexOf(indexArr[i]) === -1){
//   char.push(indexArr[i])
// }
// console.log(alphabet[indexArr[i]+13])
else if(indexArr[i] <= 12){
// console.log(indexArr[i])

// console.log(alphabet[indexArr[i]])
char.push(alphabet[indexArr[i]+13])
} else if (indexArr[i] > 12){
// char.push(alphabet[indexArr[i]-13])

diff = (indexArr[i] - 13)
char.push(alphabet[diff])

} else {char.push(indexArr[i])}
// else{
//   console.log(char)
//   char.push(indexArr[i])
//   }
//  else if(indexArr[i] === undefined){
//   console.log(indexArr[i])
// }

}
console.log(char.join("").toUpperCase())

// console.log(char.join("").toUpperCase())
return char.join("").toUpperCase()
}

rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");
``````

Solved!!
However I would love some feedback if possible, so I had to hard code some of the if conditions , whether when I was trying to capture white spaces or for some reason the letter “a” kept getting changed to a " number 0" and I have no idea why
I had to change the if condition order , never thought that was really crucial, felt like the iterator would go over all the items and just do the conditions. definitely worth some extra reading

here is my code

``````function rot13(str) {
const strArr = str.toLowerCase().split("")
let indexArr=[]
let char=[]
let diff;
let spaces=[]
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
strArr.forEach(letter => {
if(alphabet.indexOf(letter) === -1){
indexArr.push(letter)
// console.log(letter)
}

else {
//  (returns a)
//  console.log(letter)
indexArr.push(alphabet.indexOf(letter))
}
})
for (let i=0; i < indexArr.length; i++){
// console.log(indexArr)
if(indexArr[i] === 0){
char.push('n')
console.log(char)
}
else if(indexArr[i] == " " ){
//  console.log(indexArr[i])
char.push(indexArr[i])
// console.log(char)
}

else if( indexArr[i] <= 12){
// console.log(indexArr[0] + 13)
char.push(alphabet[indexArr[i]+13])
//  console.log(indexArr)
} else if (indexArr[i] > 12){

// char.push(alphabet[indexArr[i]-13])

diff = (indexArr[i] - 13)
char.push(alphabet[diff])

} else {char.push(indexArr[i])}
// else{
//   console.log(char)
//   char.push(indexArr[i])
//   }
//  else if(indexArr[i] === undefined){
//   console.log(indexArr[i])
// }

}
// console.log(char)
// console.log(char.join("").toUpperCase())
return char.join("").toUpperCase()
}

rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");

type or paste code here
``````

The most important thing is you got the mechanics.
If - else is control flow statement, thus order matter. It’s like having a few multiple size holes in a line and we’re trying to filter the balls that roll through that line. If we put the biggest hole at the first place, every ball that goes through that line will all fall in that first hole.

So, whenever possible we always put the strictest one at the first place. Sometimes, when it is not possible, we need to use `break` or `continue`. You might want to research on that.

The `brow0` problem is for sure also problem of the if-else statement in the for loop. Problem in if else is either caused by order OR how we laid out the condition.

Whenever I had a problem with if else statement, I always do this:
`console.log(<line number>, <input I want to see>, <output I want to see for that input>`
You can see how I did that in the following block. With that info we could just then add ` && indexArr[i] !== ' '){` to the first if statement and it passed the spec.

Now try change the `!==` to `!=`. We’ll get the `brow0` problem again. That means in this case, the equal OR strict equal matter for JS to process the function.

``````function rot13(str) {
const strArr = str.toLowerCase().split("")
let indexArr=[]
let char=[]
let diff;
let spaces=[]
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
strArr.forEach(letter => {
if(alphabet.indexOf(letter) === -1){
indexArr.push(letter)
// console.log(letter)
}

else {
//  (returns a)
//  console.log(letter)
indexArr.push(alphabet.indexOf(letter))
}
})
for (let i=0; i < indexArr.length; i++){
// console.log(indexArr)
/*    if(indexArr[i] === 0){
char.push('n')
console.log(char)
}
else if(indexArr[i] == " " ){
//  console.log(indexArr[i])
char.push(indexArr[i])
// console.log(char)
}

else */ if( indexArr[i] <= 12 && indexArr[i] !== ' '){
console.log(34, indexArr[i])
// console.log(indexArr[0] + 13)
char.push(alphabet[indexArr[i]+13])
//  console.log(indexArr)
} else if (indexArr[i] > 12){
console.log(39, indexArr[i])
// char.push(alphabet[indexArr[i]-13])

diff = (indexArr[i] - 13)
char.push(alphabet[diff])

} else {char.push(indexArr[i])}
// else{
//   console.log(char)
//   char.push(indexArr[i])
//   }
//  else if(indexArr[i] === undefined){
//   console.log(indexArr[i])
// }

}
// console.log(char)
// console.log(char.join("").toUpperCase())
return char.join("").toUpperCase()
}
``````

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