diff --git a/src/conditional-flow/boolean-conditions.js b/src/conditional-flow/boolean-conditions.js index 70b623d4..bac24e2b 100644 --- a/src/conditional-flow/boolean-conditions.js +++ b/src/conditional-flow/boolean-conditions.js @@ -1,10 +1,18 @@ // This function should accept a boolean value and return the string // "Well done, you passed!" if the value is true, or "Sorry, try again" // if the value is false. + function getResult (didPass) { + let result=""; - // TODO: write code in this function body to pass the tests + if(didPass){ + result = "Well done, you passed!"; + }else{ + result = "Sorry, try again"; + } + // TODO: write code in this function body to pass the tests +return result; } module.exports = { diff --git a/src/conditional-flow/multiple-conditions.js b/src/conditional-flow/multiple-conditions.js index 80420abf..32311dac 100644 --- a/src/conditional-flow/multiple-conditions.js +++ b/src/conditional-flow/multiple-conditions.js @@ -1,19 +1,26 @@ // This function should return true if num is greater // than or equal to lower AND less than or equal to upper. // Implement this with a single condition. -function isInRange (num, lower, upper) { +function isInRange(num, lower, upper) { + let result = false + if (num >= lower && num <= upper) { + result = true + } + return result // TODO: write code in this function body to pass the tests - } // This function should return true if the passed string is equal // to "Hello" or "Goodbye". Implement this with a single // if statement. -function isHelloOrGoodbye (val1) { - +function isHelloOrGoodbye(val1) { + let result = false + if (val1 == 'Hello' || val1 == 'Goodbye') { + result = true + } + return result // TODO: write code in this function body to pass the tests - } // This function should return a string that describes the provided age value. The @@ -28,8 +35,24 @@ function isHelloOrGoodbye (val1) { // 5-12 | Child // 13-19 | Teenager // 20+ | Adult -function getAgeDescription (age) { - +function getAgeDescription(age) { + let ageString =""; + if(age ===0){ + ageString="Baby"; + }else if(age>=1 && age<=4){ + ageString="Toddler"; + } + else if(age>=5 && age<=12){ + ageString="Child"; + } + else if(age>=13 && age<=19){ + ageString="Teenager"; + } + else if(age>=20){ + ageString="Adult"; + } + return ageString; + // TODO: write code in this function body to pass the tests } diff --git a/src/conditional-flow/numeric-conditions.js b/src/conditional-flow/numeric-conditions.js index 0f6656a5..5c1f796c 100644 --- a/src/conditional-flow/numeric-conditions.js +++ b/src/conditional-flow/numeric-conditions.js @@ -1,24 +1,39 @@ // TODO: Implement the functions below to make the tests pass // This function should return true if there are no elements in the array, false otherwise -function isArrayEmpty (array) { - +function isArrayEmpty(array) { + let result; + if (array == null||array.length == 0) { + result=true; + + }else{ + result = false; + } + return result; + // TODO: write code in this function body to pass the tests - } // This function should return true if num1 is greater than num2, false otherwise -function isGreaterThan (num1, num2) { - +function isGreaterThan(num1, num2) { + if(num1 > num2){ + return true; + }else{ + return false; + } // TODO: write code in this function body to pass the tests - } // This function should return the lowest number in the passed array -function findLowest (nums) { - +function findLowest(nums) { + let lowestNum = nums[0]; + for (var i = 0; i <= nums.length; i++){ + if(lowestNum>nums[i]){ + lowestNum=nums[i]; + } + } + return lowestNum; // TODO: write code in this function body to pass the tests - } module.exports = { diff --git a/src/conditional-flow/string-conditions.js b/src/conditional-flow/string-conditions.js index d91bfaf0..a8a4687f 100644 --- a/src/conditional-flow/string-conditions.js +++ b/src/conditional-flow/string-conditions.js @@ -1,45 +1,87 @@ // This function should return true if the passed string is equal to "Hello" -function isHello (val1) { - +function isHello(val1) { + if (val1 == 'Hello') { + return true + } else { + return false + } // TODO: write code in this function body to pass the tests - } // This function should return true if the passed string is not equal to "Hello" -function isNotHello (val1) { - +function isNotHello(val1) { + if (val1 != 'Hello') { + return true + } else { + return false + } // TODO: write code in this function body to pass the tests - } // This function should return true if the string val1 is is longer // than string val2 -function isLongerThan (val1, val2) { +function isLongerThan(val1, val2) { + if (val1.length > val2.length) { + return true + } else { + return false + } // TODO: write code in this function body to pass the tests - } // This function should return true if the string passed in the function's first // argument has an odd number of vowels -function hasOddNumberVowels (val1) { - - // TODO: write code in this function body to pass the tests - +function hasOddNumberVowels(val1) { + let vowels = ['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'] + let vowelsNum = 0 + let result + + for (let i = 0; i < val1.length; i++) { + for (let j = 0; j < vowels.length; j++) { + if (val1[i] == vowels[j]) { + vowelsNum = vowelsNum + 1 + } + } + } + + if (vowelsNum % 2 == 0) { + return (result = false) + } else { + return (result = true) + } } +// TODO: write code in this function body to pass the tests // this function should return the middle character of a string if it has an odd number // of characters. If there are an even number of characters the function should return // the middle two letters -function getMiddleLetter (val1) { - // TODO: write code in this function body to pass the tests +function getMiddleLetter(val1) { + let val2 = Array.from(val1) + + let valStr = '' + let valNum = val1.length + let halfStr = valNum / 2 + halfStr = halfStr.toFixed(0) + //Even String + if (valNum % 2 == 0) { + valStr = val2[halfStr - 1] + val2[halfStr] + console.log(valStr + ' ' + halfStr) + } //Odd String + else if (valNum % 2 != 0) { + valStr = val2[halfStr - 1] + } + + return valStr + // TODO: write code in this function body to pass the tests } // This function should return the name of the season for the provided -// month name. For example, "January" should return "Winter". If the provided +// month name. +//For example, "January" should return "Winter". If the provided // value is not a valid month, an empty string ("") should be returned. Use // the below ranges for each season: // @@ -47,9 +89,25 @@ function getMiddleLetter (val1) { // Summer - June to August // Autumn - September to November // Winter - December to February -function seasonForMonth (monthName) { - +function seasonForMonth(monthName) { // TODO: write code in this function body to pass the tests + + let season; + + if(monthName == 'March' || monthName == 'April' || monthName == 'May') { + season = 'Spring'; + }else if(monthName =='June' || monthName == 'July' || monthName == 'August'){ + season = 'Summer'; + }else if(monthName == 'September' || monthName == 'October' || monthName =='November'){ + season='Autumn'; + }else if(monthName == 'December' || monthName=='January'|| monthName=='February'){ + season='Winter'; + }else{ + season=""; + + + } + return season; } module.exports = { diff --git a/src/data-types/arrays/accessing-elements.js b/src/data-types/arrays/accessing-elements.js index 374a19bb..257df20b 100644 --- a/src/data-types/arrays/accessing-elements.js +++ b/src/data-types/arrays/accessing-elements.js @@ -1,19 +1,19 @@ // do not edit this section -const cities = ['London', 'Shanghai', 'New York', 'Delhi', 'Kuala Lumpur'] +const cities = ['London', 'Shanghai', 'New York', 'Delhi', 'Kuala Lumpur']; // TODO: write code to pass the tests // Set names equal to an array containing 'Bob', 'Jane', 'Joanna' in that order -const names = null +const names = ['Bob','Jane','Joanna']; // Set fourthCity to the 4th element in the cities array -const fourthCity = '' +const fourthCity = cities[3]; // Set firstCity to the 1st element in the cities array -const firstCity = '' +const firstCity = cities[0]; // Set lengthOfCitiesArray to the length of the cities array -const lengthOfCitiesArray = NaN +const lengthOfCitiesArray = cities.length; // Do not edit this exported object module.exports = { diff --git a/src/data-types/arrays/adding-removing-elements.js b/src/data-types/arrays/adding-removing-elements.js index 69e559e8..8037469b 100644 --- a/src/data-types/arrays/adding-removing-elements.js +++ b/src/data-types/arrays/adding-removing-elements.js @@ -10,25 +10,27 @@ const fruits = ['Apple', 'Orange', 'Pear'] // TODO: write code to pass the tests // Edit this code to add 'Fred' to the names array -names.push(undefined) +names.push('Fred'); // Edit this code to add 4 to the end of the numbers array -numbers.push(NaN) +numbers.push(4); // Edit this code to add 'Rio' to the start of the cities array -cities.unshift(undefined) +cities.unshift('Rio'); // Use an array method to remove the first item from colours -colours +colours.splice(0,1); // Use an array method to remove the last item from keys -keys +keys.splice(keys.length-1,1); // Use an array method to remove 'Jordon' from the countries array -countries.splice(NaN, NaN) +countries.splice(1, 1); // use an array method to remove the last item from the fruits array and store the value in the pear variable -const pear = fruits.undefined +const pear = fruits[fruits.length-1]; +fruits.splice(fruits.length-1,1); + // Do not edit this exported object module.exports = { diff --git a/src/data-types/numbers.js b/src/data-types/numbers.js index 01dd70de..6f298f9d 100644 --- a/src/data-types/numbers.js +++ b/src/data-types/numbers.js @@ -6,22 +6,23 @@ const numThree = 32 // TODO: Add code below using Javascript numeric operators so that the tests pass // Set this variable to numOne added to numTwo -const numOnePlusNumTwo = NaN +const numOnePlusNumTwo = numOne + numTwo; // Set this variable to numThree multiplied by numTwo -const numThreeTimesNumTwo = NaN +const numThreeTimesNumTwo = numThree* numTwo; // Set this variable to numThree divided by numOne -const numThreeDividedByNumOne = NaN +const numThreeDividedByNumOne = numThree/numOne; // Set this variable to numThree minus numOne -const numThreeMinusNumOne = NaN +const numThreeMinusNumOne = numThree - numOne; + // Set this variable to the sum of numOne, numTwo and numThree -const sum = NaN +const sum = numOne + numTwo + numThree; // Set this variable to the sum of (numOne, numTwo, numThree) divided by numOne -const numBytes = NaN +const numBytes = sum/numOne; // do not edit the exported object. module.exports = { diff --git a/src/data-types/objects/creating-objects.js b/src/data-types/objects/creating-objects.js index 1939e6b0..3188877c 100644 --- a/src/data-types/objects/creating-objects.js +++ b/src/data-types/objects/creating-objects.js @@ -1,7 +1,21 @@ // TODO: write code in this section to pass the tests. You will need to add new code // as well as modify some of the existing code -const person = null -const computer = null +const person = { + name: 'Jane', + age:32 +} + + +const computer = { + form: 'laptop', + specs:{ + memory:'16GB', + storage:'1TB' + + } + +} + // Do not edit this exported object module.exports = { diff --git a/src/data-types/objects/object-keys.js b/src/data-types/objects/object-keys.js index 4999e940..833d0159 100644 --- a/src/data-types/objects/object-keys.js +++ b/src/data-types/objects/object-keys.js @@ -1,26 +1,28 @@ // do not edit this section +const isbn13 = '978-0132350884' const book = { name: 'Clean Code', author: 'Robert C. Martin', - category: 'Cooking', + category: 'Programming', isbn: { isbn10: '9780132350884', - asin: '0132350882' + isbn13:isbn13 }, - publisher: 'Prentice Hall', - dimensions: '10x12x2' + pages:464, + + publisher: 'Prentice Hall' } -const isbn13 = '978-0132350884' + // TODO: write code in this section to pass the tests. You will need to add new code // as well as modify some of the existing code // Set this to the book name -const name = '' +const name = book.name; // Set this to the isbn 10 value -const isbn10 = '' +const isbn10 =book.isbn.isbn10; // Do not edit this exported object module.exports = { diff --git a/src/data-types/objects/objects-and-arrays.js b/src/data-types/objects/objects-and-arrays.js index e4819467..3366d119 100644 --- a/src/data-types/objects/objects-and-arrays.js +++ b/src/data-types/objects/objects-and-arrays.js @@ -4,12 +4,17 @@ const basket = { { name: 'Apple', quantity: 10, - price: 1 + price: 2 }, { name: 'Lemon', quantity: 2, price: 0.5 + }, + { + name: 'Oranges', + price: 0.75, + quantity:4 } ], voucherCodes: [ @@ -22,10 +27,10 @@ const basket = { // as well as modify some of the existing code // Set this variable to the length of the baskets voucher codes array -const numberOfVoucherCodes = null +const numberOfVoucherCodes =basket.voucherCodes.length; // Set this variable to the first element in of the baskets voucher codes array -const firstVoucherCode = null +const firstVoucherCode = basket.voucherCodes[0]; // Do not edit this exported object module.exports = { diff --git a/src/data-types/strings.js b/src/data-types/strings.js index a8eb8c5b..f62c86dd 100644 --- a/src/data-types/strings.js +++ b/src/data-types/strings.js @@ -6,16 +6,16 @@ const secondName = 'Smith' // TODO: Update the code using Javascript string operations and the variables above so that the tests pass. // Set this variable to firstName and secondName concatenated -const fullName = null +const fullName = firstName+" "+secondName; // Set this variable to the 10th character of the alphabet variable -const tenthCharacterOfAlphabet = null +const tenthCharacterOfAlphabet = alphabet.charAt(9); // Set this variable by calling a method on the alphabet variable to transform it to lower case -const lowerCaseAlphabet = null +const lowerCaseAlphabet = alphabet.toLowerCase(); // Set this variable by using a property on the alphabet variable to get it's length -const numberOfLettersInAlphabet = null +const numberOfLettersInAlphabet = alphabet.length; // do not edit the exported object. module.exports = { diff --git a/src/demo/demo.js b/src/demo/demo.js index 9201bd8a..68d202fc 100644 --- a/src/demo/demo.js +++ b/src/demo/demo.js @@ -4,11 +4,11 @@ const numTwo = 2 let numThree = 0 // TODO: Update numThree so the tests pass -numThree = 0 +numThree = 5; // TODO: Update the code below so that the tests pass -const numOnePlusNumTwo = 0 // Set this variable to numOne plus numTwo +const numOnePlusNumTwo = numOne+numTwo;// Set this variable to numOne plus numTwo // do not edit this section module.exports = { diff --git a/src/functions/calling-functions.js b/src/functions/calling-functions.js index f44038d6..915cb3d7 100644 --- a/src/functions/calling-functions.js +++ b/src/functions/calling-functions.js @@ -19,13 +19,13 @@ function sayHelloManyTimes (name, times) { // TODO: Add and update code here to make the tests pass // Set this variable to 'Hello' by calling the sayHello function -const hello = '' +let hello = sayHello(); // Set this variable variable to 'Hello Jane' calling the sayHelloTo function -const helloToJane = '' +let helloToJane = sayHelloTo('Jane'); // Set this variable to 'Hello Bob! Hello Bob! Hello Bob!' calling the sayHelloManyTimes function -const helloToBob3Times = '' +let helloToBob3Times = sayHelloManyTimes('Bob',3); // do not edit below this line module.exports = { diff --git a/src/functions/creating-functions-multiple-args.js b/src/functions/creating-functions-multiple-args.js index a437ae8a..ced62545 100644 --- a/src/functions/creating-functions-multiple-args.js +++ b/src/functions/creating-functions-multiple-args.js @@ -8,6 +8,19 @@ // 10, 13 | [10, 11, 12, 13] // -1, 1 | [-1, 0, 1] // +function lowerAndUpper(lower, upper){ + console.log(lower, upper); + let limitArray=[]; + let count = 0; + + for (let i = lower; i <= upper; i++) { + limitArray[count] = i; + count = count + 1; + console.log(limitArray[count]); + } + return limitArray; +} + // TODO: write code below // define a function that takes two arguments: a string and a number. @@ -21,9 +34,17 @@ // error, 10 | ERROR!!!!!!!!!! // // TODO: write code below +function concatMyString(str,number){ + let newStr = str.toUpperCase(); + for(let i = 0; i < number;i++){ + newStr = newStr.concat("!"); + } + return newStr; + +} // change the exported value to be the name of the function you defined module.exports = { - a: undefined, // change undefined to be the name of the function defined to create the range of numbers (the first todo) - b: undefined // change undefined to be the name of the function defined to return the string with exclamations (the second todo) + a: lowerAndUpper, // change undefined to be the name of the function defined to create the range of numbers (the first todo) + b: concatMyString // change undefined to be the name of the function defined to return the string with exclamations (the second todo) } diff --git a/src/functions/creating-functions.js b/src/functions/creating-functions.js index 2d2ac5e6..f75dad99 100644 --- a/src/functions/creating-functions.js +++ b/src/functions/creating-functions.js @@ -7,6 +7,9 @@ // 2 | 3 // // TODO: write code below +function incrementNumber(number){ + return number + 1; +} // Define a function that takes any person's name and returns it with a smiley :)! // Remember to make the name capitalized! @@ -17,10 +20,18 @@ // edward | Hi, Edward :) // Aiyana | Hi, Aiyana :) // +function helloEveryone(name){ + let newChar =Array.from(name); + let firstChar= newChar[0].toUpperCase(); + newChar[0] = firstChar; + let newStr = newChar.join(''); + + return "Hi, " + newStr+" :)"; +} // TODO: write code below // TODO: change undefined to be the name of the functions you defined module.exports = { - a: undefined, // change undefined to be the name of the function you defined to increment a number (the first TODO) - b: undefined // change undefined to be the name of the function you defined to say hi (the second TODO) + a: incrementNumber, // change undefined to be the name of the function you defined to increment a number (the first TODO) + b: helloEveryone // change undefined to be the name of the function you defined to say hi (the second TODO) } diff --git a/src/loops/for-loop-and-arrays.js b/src/loops/for-loop-and-arrays.js index 682995ec..8ca0d239 100644 --- a/src/loops/for-loop-and-arrays.js +++ b/src/loops/for-loop-and-arrays.js @@ -5,20 +5,52 @@ let word = '' // TODO: Add code below this line to make the tests pass -// Use a for loop to set the sum variable to the sum of all the values in nums -sum = 0 +// Use a for loop to set the sum variable to{} the sum of all the values in nums + + for (let i = 0; i < nums.length; i++) { + sum = sum + nums[i]; + } + // Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) -const doubledNums = [] + let doubledNums=[] + for (let i = 0; i < nums.length; i++) { + doubledNums[i] = nums[i]*2; + } + // Use a for loop to set word equal to all the letters in the letters array -word = '' +word = ''; +let newWord=[]; +for (let i = 0; i < letters.length; i++) { + newWord[i] = letters[i]; +} +word = newWord.join(''); // Use a for loop to populate everySecondNum with every second number from the nums array -const everySecondNum = [] +const everySecondNum = []; +let count1=0; + +for (let i = 0; i < nums.length; i++){ + + if(i%2 != 0){ + everySecondNum[count1] =nums[i]; + count1++; + } +} // Use a for loop to populate numsReversed with the numbers from nums in reverse order -const numsReversed = [] +const numsReversed = []; +let count = 0; +console.log(nums.length); +for (let i = nums.length; i > 0; i=i-1){ + numsReversed[count]=nums[i-1]; + console.log(numsReversed[count]); + count=count+1; + +} + + // do not change below this line module.exports = { diff --git a/src/loops/for-loop-basic.js b/src/loops/for-loop-basic.js index 38a5c971..5a4b755e 100644 --- a/src/loops/for-loop-basic.js +++ b/src/loops/for-loop-basic.js @@ -4,12 +4,31 @@ const evenNums = [] const countdown = [] // TODO: Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array +for (let i =0; i <4; i++){ + numsZeroToThree[i]=i; +} // TODO: Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array +let five=5; +for (let i =0; i <6; i++){ + numsFiveToTen[i]=five++; +} // TODO: Write a for loop that adds all the even numbers between 0 and 6 (0, 2, 4, 6) to evenNums +let even=0; +for (let i =0; i <4; i++){ + evenNums[i]=even; + even=even+2; +} // TODO: Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array +let count=3; +for (let i =0; i <4; i++){ + + countdown[i]=count; + count--; + +} // do not change below this line module.exports = { diff --git a/src/variables/assignment.js b/src/variables/assignment.js index 98f2e31c..aa01d00a 100644 --- a/src/variables/assignment.js +++ b/src/variables/assignment.js @@ -3,9 +3,10 @@ let firstNumber = 10 firstNumber = 0 // TODO: Set the value of firstNumber below so the tests pass +firstNumber=20; // TODO: Change the code below so that the tests pass -const secondNumber = 0 // edit this value +const secondNumber = 42; // edit this value // do not edit the exported object. module.exports = { diff --git a/src/variables/declaration.js b/src/variables/declaration.js index 8afaa55b..3e857bed 100644 --- a/src/variables/declaration.js +++ b/src/variables/declaration.js @@ -2,6 +2,8 @@ // // // TODO: Declare the variables firstName and age so that the tests pass +let firstName = 'Jane'; +let age=35; // do not edit below this line let firstNameExport = ''