Videos
Much simpler solution - use the below function
function reverseArray(arr) {
for(let idx in arr) {
arr[idx] = arr[idx].split('').reverse().join('');
}
}
const sampleArr = ['ABC', 'DEF','GHI','JKL'];
console.log('Before Reverse', sampleArr);
reverseArray(sampleArr);
console.log('After Reverse', sampleArr);
Edit:
If you don't want to change the existing array use below function
function reverseArray(arr) {
return arr.map(item => item.split('').reverse().join(''));
}
const sampleArr = ['ABC', 'DEF','GHI','JKL'];
const reverseArr = reverseArray(sampleArr);
console.log('Before Reverse', sampleArr);
console.log('After Reverse', reverseArr);
Did you mean reverseStuff(args[y]) in your code instead of return functionReverse(args)? Because, the first one will call the reverse function for each string. The second one will probably be an error since there is no function named functionReverse in your code. Even if it existed, what you are doing is calling that function with the entire array of strings every time for the length of the array args. The missing args[y] is the main issue. Here, you are accessing the value at the yth index of the array args which, is what you wanted to do. Also, as @ASDFGerte pointed out, <= should be < since the first one will return undefined.
Also note that with the above change, the function will iterate through the array but since it returns nothing, the console.log(eachword(args)) line will print undefined. In this case, you can do the following:
// inside the for loop in eachWord
console.log(reverseStuff(args[y]);
Or, the better solution will be the following:
const args = process.argv.slice(2);
let allArr = [];
let eachWord = function (args, cb) {
for (let y = 0; y < args.length; y++) {
cb(reverseStuff(args[y]));
}
};
let cb = function(reveredString) {
// do what you want here
// for example
allArr.push(reveredString);
// or just simply print
console.log(reversedString);
};
eachWord(args, cb);
Hope this helps!