pls help me with this error …I am trying to make a program by my own logic to sort words in alphabetical order in javascript.Here’s my code …its incomplete as I am experiencing an error .I am not able to access i in the forEach method in the very last step idk y …can someone pls help me clear that up??
//sort words in alphabetical order
import readline from “readline-sync”
let string=readline.question(“Enter the words:”)
let asciino=
let lower=
let words=string.split(" ")
words.forEach((x)=>{
lower.push(x.toLowerCase())
})
lower.forEach((x)=>{
asciino.push(x.charCodeAt(0))
})
asciino.sort((a,b)=>a-b)
console.log(asciino)
console.log(lower)
let i=0
lower.forEach((x)={
//while(i<=asciino.length-1){
// if(x.charCodeAt(0)==asciino[i]){
// }
// i++
//}
while(i<=asciino.length-1){//here's the error the i here is showing the error
//mentioned before
//random code
};
})
console.log(final)
Edit: Just noticed you are new so welcome to the forum.
The problem is the arrow function.
Basically you have an odd assignment there instead of a fat arrow function “=>”. Because the “>” is missing javascript does best it can with it. ''forEach" expects a callback function as a parameter.
lower.forEach((x) = {
you want instead
lower.forEach((x) => {
Is the last forEach I am talking about.
PS:
“final” does not exist. Remove this console.log(final);
// sort words in alphabetical order
import readline from 'readline-sync';
let string = readline.question('Enter the words:');
let asciino = [];
let lower = [];
let words = string.split(" ");
words.forEach((x) => {
lower.push(x.toLowerCase())
});
lower.forEach((x) => {
asciino.push(x.charCodeAt(0));
});
asciino.sort((a, b) => a - b);
console.log(asciino);
console.log(lower);
let i = 0;
lower.forEach((x) => {
while (i <= asciino.length - 1) {
if (x.charCodeAt(0) == asciino[i]) {
console.log('do something ');
}
i++;
}
while (i <= asciino.length - 1) {//here's the error the i here is showing the error
//mentioned before
//random code
};
});
// console.log(final);