Home > Net >  How JS forEach works with ternary and if operators?
How JS forEach works with ternary and if operators?

Time:12-31

I am trying to understand about 2 hours what I am doing wrong in third and fourth line of code. The console thows out SyntaxError: Unexpected token if or SyntaxError: missing ) after argument list. The first and the second line works as expected.

let arr = ['Custy Stomer', 'C. Oostomar', 'C.U.S. Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur'];

let checkArr = (arr) => arr.forEach(el => console.log(typeof el !== 'string'))

let checkArr = (arr) => arr.forEach(el => if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`)} )

let checkArr = ((arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null; )})

checkArr(arr);

CodePudding user response:

Line 3:

Wrap the forEach callback body in brackets

let checkArr = (arr) => arr.forEach(el => {
  if (typeof el !== 'string') { 
    console.log(`Type error: ${el} should be a string!`);
  } 
})

Line 4:

Remove semi-colon

let checkArr = (arr) => { 
  arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null )
}

Recommended:

const checkArr = (arr) => {
  arr.forEach(el => {
    if (typeof el !== 'string') { 
      console.log(`Type error: ${el} should be a string!`);
    } 
  });
}

CodePudding user response:

Hello you defined your function with an extra (

Solution for 3rd case:

let arr = ['Custy Stomer', 'C. Oostomar', 'C.U.S. Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur'];

let checkArr = (arr) => {
  arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null
  )
}

checkArr(arr);

CodePudding user response:

Two issues.

  • Second functiopn definition should be written inside {}
  • Thirst statement, remove unwanted semi column after null.

let arr = ['Custy Stomer', 'C. Oostomar', 'C.U.S. Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur'];
let checkArr1 = (arr) => arr.forEach(el => console.log(typeof el !== 'string'))
let checkArr2 = (arr) => arr.forEach(el => { if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`) } })
let checkArr3 = ((arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null) })
checkArr1(arr);
checkArr2(arr);
checkArr3(arr);

CodePudding user response:

let arr = ['Custy Stomer', 'C. Oostomar', 'C.U.S. Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur'];

let checkArr = (arr) => arr.forEach(el => console.log(typeof el !== 'string'))

let checkArr = (arr) => arr.forEach(el => {if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`)}} )

let checkArr = ((arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null )})

checkArr(arr);

CodePudding user response:

While writing the arrow function, if you're not returning the result in the single line then you should better wrap it inside the curly brackets. You haven't wrapped it in the curly brackets in the second case.

And for the third case you've an useless ';'.

I've fixed both the lines.

let arr = ['Custy Stomer', 'C. Oostomar', 'C.U.S. Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur'];

let checkArr = (arr) => arr.forEach(el => console.log(typeof el !== 'string'))

let checkArr = (arr) => arr.forEach(el => {if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`)} })

let checkArr = ((arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null )})

checkArr(arr);

CodePudding user response:

just missing some Braces so everythings fine i think...

let arr = ['Custy Stomer', 'C. Oostomar', 'C.U.S. Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur'];
console.log(arr);
var checkArr = (arr) => arr.forEach(el => console.log(typeof el !== 'string'));

var checkArr = (arr) => {
    arr.forEach(el => {
        if (typeof el !== 'string') {
            console.log(`Type error: ${el} should be a string!`);
        }
    });
}

var checkArr = (arr) => {
    arr.forEach(el => {
        typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null;
    })
}

checkArr(arr);

  • Related