lalitkumarthakur / javascripthelpbylalit

The Complete JavaScript Course 2023: From Zero to Expert! from udemy. Learning javascript using this course.
https://lalitkumarthakur.github.io/javascripthelpbylalit/
BSD 3-Clause "New" or "Revised" License
0 stars 2 forks source link

Output is not as Expected in Array script.js file. #88

Open lalitkumarthakur opened 11 months ago

lalitkumarthakur commented 11 months ago

Problem - Output is returning only single character in 3d array in JS where as the output should be "Each and every element should be printed of that 3d array".

Output - A single character.

Chapter- Ch-9 Arrays.

Line no- 170 - script.js file.

VishalD51 commented 11 months ago

@lalitkumarthakur You are doing great stuff. I can contribute to this issue. Can you please explain more this so I can work on this?

What is expected output ? Should it print all age from 3rd array or something else ?

lalitkumarthakur commented 11 months ago

@VishalD51 Thanks a lot for appreciating! You need to print all the elements present inside tge arrays including name, age, true/false and their sur name.

They should be printed one by one similar to arr[i] done in java in 1d array.

Let's understand within example-

If a 1D array has 5 elements inside it then all five elements has to be printed one by one and

If a 3D array has 5 elements inside it and inside them another 5 elements then inside them another 5 elements are there then all the elements should be printed one by one in their order.

VishalD51 commented 11 months ago

@lalitkumarthakur Can you please check and Merge this PR: https://github.com/lalitkumarthakur/thapatechnicalJavaScript_course/pull/89

VishalD51 commented 11 months ago

@lalitkumarthakur Can you please check this : https://github.com/lalitkumarthakur/thapatechnicalJavaScript_course/pull/90

VishalD51 commented 11 months ago

@lalitkumarthakur , Can you please check? I have updated the code formated as per your standard.

prashant14589 commented 11 months ago

I think there are 2 issues : 1) The main array of first name has 7 first names and the surname array is 1 less and there is no explicit mention as to how to handle that 2) The code suggested and the code now being done/ added is static in nature.

Try something like below :

const arr = [ "Sneha", "Shreya", "Arup", "Priyanka", "Vinit", "Vanshika", "Olyvia", ["chatterjee", "bogopaddhayai", "grop", "singh", "verma", "Cornelo"], ]

const arrLen = arr.length; let i = 0, counter = 0; let arrMapper = new Map(); for(let i = 0; i< arr.length;i++){ if(Array.isArray(arr[i])) { arrMapper.set(arrMapper.size, arr[i]); counter ++; } } // Counter is to measure how many less iterations we need to do console.log("Array Mapper", arrMapper, counter); for(let i = 0; i< arr.length - counter;i++){ // Can add in a function to get the values from the mapper, but keeping it simple for now console.log(arr[i] + " "+ arrMapper.get(0)[i]) }

So, the idea here is : if the value at ant given index has an array, just create an array and then pick values from that array, and also to optimise, just have a counter which reduces the number of iterations from original loop

or you can simply just use :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat -- which will give you the required array for printing values

VishalD51 commented 10 months ago

@lalitkumarthakur It seems like you have changed the folder structure. Can you please specify updated file ?

lalitkumarthakur commented 10 months ago

@VishalD51 Yes, the folder structure had been fixed. I did improper planing so that create issues and I had to change the folder structure.

Please copy your changes to a new file (backup), then do git checkout . to discard all your changes, do git fetch then git pull main to fetch and pull the latest changes,

now switch to correct-v2 branch, Paste your changes then upload it so that I can merge in main Branch.

Please explain the code.

lalitkumarthakur commented 10 months ago

I think there are 2 issues :

1. The main array of first name has 7 first names and the surname array is 1 less and there is no explicit mention as to how to handle that

2. The code suggested and the code now being done/ added is static in nature.

Try something like below :

const arr = [ "Sneha", "Shreya", "Arup", "Priyanka", "Vinit", "Vanshika", "Olyvia", ["chatterjee", "bogopaddhayai", "grop", "singh", "verma", "Cornelo"], ]

const arrLen = arr.length; let i = 0, counter = 0; let arrMapper = new Map(); for(let i = 0; i< arr.length;i++){ if(Array.isArray(arr[i])) { arrMapper.set(arrMapper.size, arr[i]); counter ++; } } // Counter is to measure how many less iterations we need to do console.log("Array Mapper", arrMapper, counter); for(let i = 0; i< arr.length - counter;i++){ // Can add in a function to get the values from the mapper, but keeping it simple for now console.log(arr[i] + " "+ arrMapper.get(0)[i]) }

So, the idea here is : if the value at ant given index has an array, just create an array and then pick values from that array, and also to optimise, just have a counter which reduces the number of iterations from original loop

or you can simply just use :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat -- which will give you the required array for printing values

Thanks for the idea, but the problem statement is to

"Print all the elements of a 3D array"

But you have modified it to 2D array, so this will not be approved.

If you can print all the elements of the 3D array, then please help.