Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions exercises/B-hello-world/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
console.log("Hello world");

//* Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'.
console.log('Hello World. I just started learning JavaScript!');

// * Try to console.log() several things at once.
let greeting = "Hello";
let firstName = "Kara";
console.log(`${greeting} ${firstName}`);

//* What happens when you get rid of the quote marks?
//console.log(Hello, World!); SyntaxError: missing ) after argument list

//* What happens when you console.log() just a number without quotes?
console.log(12345);
7 changes: 5 additions & 2 deletions exercises/C-variables/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Start by creating a variable `greeting`

console.log(greeting);
var greeting = "Hello world";
//* Print your `greeting` to the console 3 times
for (i = 0; i < 3; i++){
console.log(greeting);
}
4 changes: 3 additions & 1 deletion exercises/D-strings/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

//* Write a program that logs a message and its type
let message = "This is a string";
console.log(message);
console.log(typeof message);
5 changes: 4 additions & 1 deletion exercises/E-strings-concatenation/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Start by creating a variable `message`

//* Write a program that logs a message with a greeting and your name
let greetingStart = "Hello, my name is ";
let myName = "Kara";
let message = greetingStart + myName;
console.log(message);
4 changes: 3 additions & 1 deletion exercises/F-strings-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

//* Log a message that includes the length of your name
let myName = "Kara";
let message = `My name is ${myName} and my name is ${myName.length} characters long`;
console.log(message);
7 changes: 5 additions & 2 deletions exercises/F-strings-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const name = " Daniel ";
//* Log the same message using the variable, `name` provided
//* Use the `.trim` method to remove the extra whitespace

console.log(message);
const name = " Daniel ";
let message = `My name is ${name.trim()} and my name is ${name.length} characters long`;
console.log(message);
6 changes: 6 additions & 0 deletions exercises/G-numbers/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// 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
let numberOfStudents = 15;
let numberOfMentors = 8;
let total = numberOfStudents + numberOfMentors
console.log(`Number of Students: ${numberOfStudents}\nNumber of Mentors: ${numberOfMentors}\nTotal number of students and mentors: ${total}`);
7 changes: 7 additions & 0 deletions exercises/I-floats/exercise.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
var numberOfStudents = 15;
var numberOfMentors = 8;

//* Using the variables provided in the exercise calculate the percentage of mentors and students in the group
let total = numberOfStudents + numberOfMentors;
let percentageStudents = Math.round((numberOfStudents / total) * 100);
let percentageMentors = Math.round((numberOfMentors / total) * 100);
console.log(`Percentage students: ${percentageStudents}%`);
console.log(`Percentage mentors: ${percentageMentors}%`);
10 changes: 8 additions & 2 deletions exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
//* Complete the function in exercise.js so that it halves the input
//* Try calling the function more than once with some different numbers

function halve(number) {
// complete the function here
return number / 2;
}

var result = halve(12);

console.log(result);
result = halve(14);
console.log(result);
result = halve(16);
console.log(result);
4 changes: 3 additions & 1 deletion exercises/J-functions/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//* 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);
Expand Down
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//* 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`
Expand Down
5 changes: 4 additions & 1 deletion exercises/K-functions-parameters/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//* From scratch, write a function that divides two numbers
// Declare your function first

function divide(num1, num2){
return num1 / num2;
}
var result = divide(3, 4);

console.log(result);
5 changes: 4 additions & 1 deletion exercises/K-functions-parameters/exercise3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//* Write a function that takes a name (a string) and returns a greeting
// Write your function here

function createGreeting(name){
return `Hello, my name is ${name}`;
}
var greeting = createGreeting("Daniel");

console.log(greeting);
10 changes: 7 additions & 3 deletions exercises/K-functions-parameters/exercise4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//* 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 addNumbers(num1, num2){
return num1 + num2;
}
// Call the function and assign to a variable `sum`

console.log(sum);
let sum = addNumbers(13, 124);
console.log(sum);
4 changes: 4 additions & 0 deletions exercises/K-functions-parameters/exercise5.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//* Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string)
// Declare your function here
function createLongGreeting(name, age){
return `Hello, my name is ${name} and I am ${age} years old`;
}

const greeting = createLongGreeting("Daniel", 30);

Expand Down
17 changes: 12 additions & 5 deletions exercises/L-functions-nested/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
var mentor1 = "Daniel";
var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";
//- In `exercise.js` write a program that displays the percentage of students and mentors in the group
//- The percentage should be rounded to the nearest whole number (use a search engine to find out how to this with JavaScript)
//- You should have one function that calculates the percentage, and one function that creates a message

function percentageOfStudentsAndMentors(numberOfStudents, numberOfMentors){
let total = numberOfStudents + numberOfMentors;
let percentageStudents = Math.round((numberOfStudents / total) * 100);
let percentageMentors = Math.round((numberOfMentors / total) * 100);
return `Percentage mentors: ${percentageStudents}%\nPercentage students: ${percentageMentors}%`;
}

console.log(percentageOfStudentsAndMentors(15, 8));
13 changes: 13 additions & 0 deletions exercises/L-functions-nested/exercise2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//- In `exercise2.js` you have been provided with the names of some mentors. Write a program that logs a shouty greeting to each one.
//- Your program should include a function that spells their name in uppercase, and a function that creates a shouty greeting.
//- Log each greeting to the console.

function shoutyGreeting(mentorName){
return `hello ${mentorName}`.toUpperCase();
}

let mentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"];

for (i = 0; i < 5; i++){
console.log(shoutyGreeting(mentors[i]));
}
11 changes: 8 additions & 3 deletions extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}

function convertToUSD(priceInPounds) {
return priceInPounds * 1.4;
}
console.log(convertToUSD(32));
/*
CURRENCY FORMATTING
===================
Expand All @@ -15,7 +17,10 @@ function convertToUSD() {}
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(priceInPounds) {
return (priceInPounds * 0.99) * 5.7;
}
console.log(convertToBRL(30));

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
25 changes: 15 additions & 10 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@
the final result to the variable goodCode
*/

function add() {

function add(num1, num2) {
return num1 + num2;
}
console.log(add(1, 3));

function multiply() {

function multiply(num1, num2) {
return num1 * num2;
}
console.log(multiply(2, 3));

function format() {

function format(number) {
return `£${number}`;
}
console.log(format(16));

const startingValue = 2
const startingValue = 2;

// Why can this code be seen as bad practice? Comment your answer.
let badCode =
let badCode = add(startingValue, 10); badCode = multiply(badCode, 2); console.log(badCode = format(badCode)); // code all on one line is difficult to read

/* BETTER PRACTICE */

let goodCode =
// Comments can be added to explain each line and it is easier to read
let goodCode = add(startingValue, 10); // function call to add 10 to startingValue and store result in goodCode
goodCode = multiply(goodCode, 2); // function call to multiply value of goodCode by 2
console.log(goodCode = format(goodCode)); // function call to format value of goodCode
Comment on lines +40 to +43
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Kara ✨ One suggestion rather than reassigning goodCode when you want to call another function, you can nest function calls for example:
let goodCode = format(multiply(add(startingValue, 10), 2));
This still gives the same result but this is an alternative solution and is all done to preference.

Keep up the good work


/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
66 changes: 64 additions & 2 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,53 @@

// This should log "The ball has shaken!"
// and return the answer.

// Object "possibleAnswers" declared for the 4 categories to set up key:value pairs
let possibleAnswers = {
veryPositive: ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", "You may rely on it."],
positive: ["As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes."],
negative: ["Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again."],
veryNegative: ["Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."]
}

// Random number function
function getRandomNumber(min, max) {
// Produces a random decimal number between 0-1 then multiplies it by the range then adds the min value. Rounds to nearest number.
return Math.round(Math.random() * (max - min) + min);
}

// Variable used in shakeBall() function and checkAnswer function to select an answer from the array of the chosen category (passes in a random number between 0 and 4)
let answerSelection = getRandomNumber(0 , 4);

function shakeBall() {
//Write your code in here
console.log("The ball has shaken!");
let randomNumber = getRandomNumber(1, 4);
let answer = "";

if (randomNumber == 1) {
// if the random number selected is 1, then variable "answer" is assigned the value of a random selection (0-4) from the "veryPositive" values array in the possibleAnswers object
answer = possibleAnswers.veryPositive[answerSelection];
return(answer);
}
else if (randomNumber == 2) {
// if the random number selected is 2, then variable "answer" is assigned the value of a random selection (0-4) from the "positive" values array in the possibleAnswers object
answer = possibleAnswers.positive[answerSelection];
return(answer);
}
else if (randomNumber == 3) {
// if the random number selected is 3, then variable "answer" is assigned the value of a random selection (0-4) from the "negative" values array in the possibleAnswers object
answer = possibleAnswers.negative[answerSelection];
return(answer);
}
else if (randomNumber == 4) {
// if the random number selected is 4, then variable "answer" is assigned the value of a random selection (0-4) from the "veryNegative" values array in the possibleAnswers object
answer = possibleAnswers.veryNegative[answerSelection];
return(answer);
}
}

console.log(shakeBall());

/*
This function should say whether the answer it is given is
- very positive
Expand All @@ -58,10 +101,29 @@ function shakeBall() {

This function should expect to be called with any value which was returned by the shakeBall function.
*/
function checkAnswer(answer) {

function checkAnswer(answer, answerSelection) {
//Write your code in here
// if the "veryPositive" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "very positive"
if (possibleAnswers.veryPositive.includes(answer, [answerSelection])) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well done Kara. you used includes method as exactly as described in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes. Your codes are brilliant!
👏👏

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.

Thank you for all your helpful suggestions :)

return "very positive";
}
// if the "positive" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "positive"
else if (possibleAnswers.positive.includes(answer, [answerSelection])) {
return "positive";
}
// if the "negative" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "negative"
else if (possibleAnswers.negative.includes(answer, [answerSelection])) {
return "negative";
}
// if the "veryNegative" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "very negative"
else if (possibleAnswers.veryNegative.includes(answer, [answerSelection])) {
return "very negative";
}
}

checkAnswer();
console.log();
/*
==================================
======= TESTS - DO NOT MODIFY =====
Expand Down
11 changes: 6 additions & 5 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// There are syntax errors in this code - can you fix it to pass the tests?

function addNumbers(a b c) {
function addNumbers(a, b, c) {
return a + b + c;
}

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";
}

function getTotal(a, b) {
total = a ++ b;
total = a + b;

return "The total is total"
return "The total is " + total;
}

/*
Expand Down
7 changes: 3 additions & 4 deletions mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// The syntax for this function is valid but it has an error, find it and fix it.

function trimWord(word) {
return wordtrim();
return word.trim();
}

function getWordLength(word) {
return "word".length();
return word.length;
}

function multiply(a, b, c) {
a * b * c;
return;
return a * b * c;
}

/*
Expand Down
Loading