diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..298d8547e 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ console.log("Hello world"); + +console.log('Hello World. I just started learning JavaScript!'); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..dda09fb77 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,3 @@ // Start by creating a variable `greeting` - -console.log(greeting); +var greeting = "Hello world\n"; // added backspace \n newline +console.log(greeting.repeat(3)); // added repeat 3 diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..ef45b58fa 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +var message = "This is a string"; +var stringType = typeof message; console.log(message); +console.log(stringType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..9711b61b5 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` +var greetingStart = "Hello, my name is "; +var name = "Jay!"; + +var message = greetingStart + name; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..9086d8eb9 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,8 @@ // Start by creating a variable `message` +var name = "Jonathan"; // 8 letters +var nameLength = name.length; +var greetingStart = "Hello, my name is Jonathan and my name is " + nameLength + " letters long."; +var message = greetingStart; + console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..cf3c1c9dc 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,8 @@ -const name = " Daniel "; +var name = " Daniel "; // 6 letters long +var nameTrim = name.trim(); // need to trim the string first! +var nameTrimLength = nameTrim.length; +var greetingStart = "Hello, my name is Daniel and my name is "; +var greetingEnd = " letters long."; -console.log(message); + +console.log(greetingStart + nameTrimLength + greetingEnd ); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..3ceacc8c5 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,20 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` + +//Create two variables `numberOfStudents` and `numberOfMentors` +//Log a message that displays the total number of students and mentors +//Number of students: 15 +//Number of mentors: 8 +//Total number of students and mentors: 23 + + +var numberOfStudents = "Number of students: "; +var numberOfMentors = "Number of mentors: "; +var numberOfStudentsMentors = "Total number of students and mentors:"; +var students = 30; +var mentors = 5; +var totalClass = students + mentors; // 35 + +console.log(numberOfStudents + students); +console.log(numberOfMentors + mentors); +console.log(numberOfStudentsMentors + totalClass); + diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..6b1391fb7 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,22 @@ +//Percentage students: 65% +//Percentage mentors: 35% + + var numberOfStudents = 15; var numberOfMentors = 8; +var studentsPer = "Percentage students: "; +var mentorsPer = "Percentage mentors: "; +var per = "%" +var total = numberOfStudents + numberOfMentors; // 23 +//function for percentage +function percentage (x,y) { + return x/y*100; + +}; +var perRoundStud = Math.round(percentage(numberOfStudents,total)); // rounded up +var perRoundMentor = Math.round(percentage(numberOfMentors,total)); // rounded up + +console.log(studentsPer + percentage(numberOfStudents,total) + per); +console.log(mentorsPer + percentage(numberOfMentors,total) + per ); +console.log(studentsPer + perRoundStud + per); // round down +console.log(studentsPer + perRoundMentor + per); // round up \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..a8d91eee5 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,21 @@ + +//Complete the function in exercise.js so that it halves the input + +//Try calling the function more than once with some different numbers + +// Remember to use the return keyword to get a value out of the function + + + + + function halve(number) { - // complete the function here + return number/2; } var result = halve(12); +var result2 = halve(50); + -console.log(result); +console.log(result); // half 12 = 6 +console.log(result2); // half 50 = 25 diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..ff303a3b8 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,12 @@ + +//Complete the function in exercise2.js so that it triples the input + + function triple(number) { // complete function here + return number*3; } -var result = triple(12); +var result = triple(12); // 12 x 3 = 36 console.log(result); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..d87d4b1a4 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,20 @@ + +//Write a function that multiplies two numbers together + + + + // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1,num2) { // Calculate the result of the function and return it + return num1 * num2; + } // Assign the result of calling the function the variable `result` var result = multiply(3, 4); +var result2 = multiply(6, 9); + +console.log(result); // 12 3x4=12 +console.log(result2); // 54 6x9=54 -console.log(result); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..c5418663c 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,13 @@ // Declare your function first +//From scratch, write a function that divides two numbers + +function divide (num1,num2) { + return num1/num2; +} + var result = divide(3, 4); +var result2 = divide(100, 3); -console.log(result); +console.log(result); // output 0.75 +console.log(Math.round(result2)); // rounded to 33 for 33.33333 diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..55abdf61e 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,16 @@ // Write your function here +//Write a function that takes a name (a string) and returns a greeting -var greeting = createGreeting("Daniel"); +var daniel ="Daniel"; +var susan ="Susan"; +var end = "!"; +function createGreeting (name) { + +return "Hello, my name is " + name + end; +} + + +var greeting = createGreeting(daniel);// added val Daniel +var greeting = createGreeting(susan); // replaces Daniel with susan console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..194b82f40 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,14 @@ + +//Write a function that adds two numbers together +//Call the function, passing `13` and `124` as parameters, and assigning the returned value to a variable `sum` + + // Declare your function first +function addition (num1,num2){ + return num1+num2; +} // Call the function and assign to a variable `sum` +var sum = addition(13,124); // 13+124 = 134 console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..f57ede92b 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,24 @@ + +//Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) + +//## Expected result + +//Hello, my name is Daniel and I'm 30 years old + + + + + // Declare your function here + + +function createLongGreeting(name,age){ + +return "Hello, my name is " + name + " and I'm " + age + " years old"; + +} + const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..f1a193470 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,26 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; + + //Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) + +// ## Expected result + +//Hello, my name is Daniel and I'm 30 years old + + + +// main function +function createGreeting (name,age){ + return "Hello, my name is " + name + " and I'm " + age + " years old" +} +// array of names +var array1 = ["Daniel","Irina","Mimi","Rob", + "Yohannes"]; + + function randomAge (min,max){ + return Math.random() * (min + max) + min; + } +var random = array1[Math.floor(Math.random()*array1.length)]; // picks names from array +var ages = randomAge(10 , 60); // randoms age +var agesRound = Math.round(ages); // rounds age +const greeting = createGreeting(random,agesRound); + +console.log(greeting); \ No newline at end of file diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..9a307ddfe 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,17 +5,21 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(GDP) { +return GDP * 1.4; +} -/* - CURRENCY FORMATTING + /*CURRENCY FORMATTING =================== The business is now breaking into the Brazilian market Write a new function for converting to the Brazilian real (exchange rate is 5.7 BRL to £) They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL. */ -function convertToBRL() {} +function convertToBRL(BRL) { + + return BRL/100*99* 5.7; // 99% of BRL X 5.7 = pounds +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/2-piping.js b/extra/2-piping.js index 067dd0f5b..1f1f94c06 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,33 @@ the final result to the variable goodCode */ -function add() { - +function add(a,b) { +var addTotal = a + b; +return addTotal; } -function multiply() { - +function multiply(c,d) { +var multiplyTotal = c * d; +return multiplyTotal; } -function format() { - +function format(num1) { + return "£" + num1; } -const startingValue = 2 +const startingValue = 2; +let addedTotal = add(startingValue, 10); +let multiTotal = multiply(addedTotal, 2); // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format(add(multiply(startingValue,2),20)); + +//looks messy hard to unerstand unsure if this is the right way??? /* BETTER PRACTICE */ -let goodCode = +let goodCode = format(multiTotal); +// more layed out. maybe a easier way to do this /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index e32182cfe..4735f2cf7 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -47,8 +47,10 @@ // and return the answer. function shakeBall() { //Write your code in here + return "The ball has shaken!" + checkAnswer; + } - +console.log(shakeBall); /* This function should say whether the answer it is given is - very positive @@ -60,8 +62,18 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here + var array1 = ["very positive" , "positive" , "negative" , "very negative"]; + var random = array1[Math.floor(Math.random()*array1.length)]; // picks names from array + //var veryPos = ["It is certain" , "It is decidedly so" , "Without a doubt" , "Yes - definitely" ,"You may rely on it"]; + //var randomVP = veryPos[Math.floor(Math.random()*veryPos.length)]; // picks names from array + //var pos = [ "As I see it, yes" , "Most likely" , "Outlook good" , "Yes" ,"Signs point to yes"]; + //var negative =[]; + //var very negative = []; + + return random; } + /* ================================== ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..63adf185a 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,29 @@ -// There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { + +//Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) + +//## Expected result + + + + + +// There are syntax errors in this code - can you fix it to pass the tests? +// addNumber adds a,b,c missing comma's +function addNumbers(a , b , c) { return a + b + c; } +// introduce adds a string with age and name missing + , missing curly brackets, added spaces +function introduceMe(name, age){ +return "Hello, my name is " + name + " and I am " + age + " years old"; +} -function introduceMe(name, age) -return "Hello, my name is " + name "and I am " age + "years old"; - +// getTotal adds a,b +//removed a + (was a ++ b) function getTotal(a, b) { - total = a ++ b; + return "The total is " + (a+b); + - return "The total is total" } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 3c578ad87..4cc2ee04b 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,23 @@ // The syntax for this function is valid but it has an error, find it and fix it. function trimWord(word) { - return wordtrim(); + //return wordtrim(); + return word.trim(); } + function getWordLength(word) { - return "word".length(); + + //return "word".length(); + return word.length; + } function multiply(a, b, c) { - a * b * c; - return; + //a * b * c; + //return; + + return (a*b*c); } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index c9221a200..3d70a280c 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -3,16 +3,31 @@ function getNumber() { return Math.random() * 10; } +// picking a random number generator, multiplying by 10 + // Add comments to explain what this function does. You're meant to use Google! function s(w1, w2) { return w1.concat(w2); } +// concatenate two words + function concatenate(firstWord, secondWord, thirdWord) { + return firstWord.concat( " ",secondWord ," " ,thirdWord); + + +//return ("I", "like", "pizza"), + // ("I like pizza"); + +//return ("I", "am", 13), + // "I am 13" + // Write the body of this function to concatenate three words together. // Look at the test case below to understand what this function is expected to return. } + + /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== @@ -43,6 +58,7 @@ test( concatenate("code", "your", "future"), "code your future" ); + test( "concatenate function - case 2 works", concatenate("I", "like", "pizza"), diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..96b243e15 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,9 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(item) { + return item/100*20+item; +} /* CURRENCY FORMATTING @@ -17,7 +19,9 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(item) { + return "£" + (item/100*20+item).toFixed(2); +} /* ===================================================