-
Notifications
You must be signed in to change notification settings - Fork 15
Js exercises bhavana #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
ffc170d
6a51502
27dbb45
273d784
21b0aa0
14cdd16
57abb09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| // Complete the below function. Find the length of the given array. | ||
| // - Also validate the input. Accept the input only if its an array. | ||
|
|
||
| function findTheLength(inputArray) { | ||
| let lengthOfArray; | ||
| let lengthOfArray = inputArray.length; | ||
| console.log(`Length of the given input array is ${lengthOfArray}`); | ||
| } | ||
| let inputArray = [1, 2, 3, 4, 5]; | ||
| Array.isArray(inputArray) && findTheLength(inputArray); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| // Complete the below function. | ||
| // - Print the given Array | ||
|
|
||
| function printArray(inputArray) {} | ||
| function printArray(inputArray) { | ||
| console.log(inputArray); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the correct solution. But try doing it with loops. |
||
| } | ||
|
|
||
| printArray([1, 2, 3, 4]); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,15 @@ | |
| // - Use Array methods. | ||
| // - Do not use loops | ||
|
|
||
| //Here we will be using map method which it returns an new array with modified elemts in the result array | ||
| //map method is simialr to forEach where forEach will not return any array as a result | ||
| function multiplyArrayByTwo(inputArray) { | ||
| // inputArray = inputArray.map(function (value) { | ||
| // return value * 2; | ||
| // }); | ||
|
|
||
| //using array function in the map methods | ||
| inputArray = inputArray.map((value) => value * 2); | ||
| console.log(`Given input array is ${inputArray}`); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print before and after arrays. Also do not mutate the array. Take the output into a new array. Do you think map function mutates the array or creates a new one?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. map method will create a new array and return it |
||
| } | ||
| multiplyArrayByTwo([1, 2, 34, 4]); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation should always go into the function. Or else you have to write the validation everytime you call it. Move it into the function.