Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/exercises/1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// - Fix the issue.

function printOneToTen() {
for (const number = 1; number <= 10; number++) {
for (let number = 1; number <= 10; number++) {
console.log(`\n${number}`);
}
}
printOneToTen();
33 changes: 20 additions & 13 deletions src/exercises/10/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
// - Do not use loops

// Example of inputArray
/*
[
{
name:"Ravi",
age:28
},
{
name:"Teja",
age:28
}
]
*/
function findVoteEligibleCandidates(populationList) {}

const voters = [
{
name: "Ravi",
age: 28,
},
{
name: "Teja",
age: 8,
},
];

function findVoteEligibleCandidates(populationList) {
// const eligibleVoters = populationList.filter(function (person) {
// return person.age > 18;
// });
const eligibleVoters = populationList.filter((person) => person.age > 18);
console.log(eligibleVoters);
}
findVoteEligibleCandidates(voters);
8 changes: 6 additions & 2 deletions src/exercises/2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
// - Fix the issue.

function divideTenByNumber(number) {
let result;
if (number != 0) {
let result = 10 / number;
result = 10 / number;
} else {
let result = "Indeterminate";
result = "Indeterminate";
}

console.log(`Result after dividing 10 by ${number} is ${result}`);
}
divideTenByNumber(3);

//result is defined but it is not in the scope of the function it is the scope of the if else block
17 changes: 11 additions & 6 deletions src/exercises/3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ function checkIfArray(input) {
let isInputAnArray = false;

//...

if (isInputAnArray) {
console.log("Given input is an Array");
} else {
console.log("Given input is not an Array");
}
isInputAnArray = Array.isArray(input);
isInputAnArray
? console.log("Given input is an Array")
: console.log("Given input is not an Array");
//used conditional operator
// if (isInputAnArray) {
// console.log("Given input is an Array");
// } else {
// console.log("Given input is not an Array");
// }
}
checkIfArray([1, 2, 3, 4, 5, 6]);
4 changes: 4 additions & 0 deletions src/exercises/4/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

function greeting(firstName, lastName) {
let welcomeGreeting;
welcomeGreeting = `${firstName} ${lastName}`;

console.log(welcomeGreeting);
}
greeting("Bhavana", "Kondeti");

// template literal can be used to used to dispaly the variable values in the text
5 changes: 4 additions & 1 deletion src/exercises/5/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// - Comment the function that you used.

function binaryToDecimal(binaryString) {
let decimalValue;
let decimalValue = parseInt(binaryString, 2);
console.log(
`Decimal for the given binary string ${binaryString} is ${decimalValue}`
);
}
binaryToDecimal("1100");
//used parseInt with the radix set to 2 ;
//parseInt(binaryString, 2);
5 changes: 3 additions & 2 deletions src/exercises/6/exercise.js
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);
Copy link
Copy Markdown
Owner

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.

4 changes: 4 additions & 0 deletions src/exercises/7/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// - Use Array methods

function commaSeparatedString(inputArray) {
//using join function for this
let joinedString = inputArray.join(",");
console.log(joinedString);
// Given an Array like ["firstName","lastName"]
// Return a comma separated String like "firstName,lastName"
}
commaSeparatedString(["hello", "Bhavana", "kondeti"]);
6 changes: 5 additions & 1 deletion src/exercises/8/exercise.js
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);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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]);
9 changes: 9 additions & 0 deletions src/exercises/9/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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]);