From dedece1411fa41869d6fc6471a8ca9897a55cbf8 Mon Sep 17 00:00:00 2001 From: Abdiasiis halane <112101372+abdiasiis78@users.noreply.github.com> Date: Tue, 21 Feb 2023 10:26:56 +0300 Subject: [PATCH] finished --- .vscode/settings.json | 3 + arrays.js | 105 +++++++++++++++++++++++++------ basics.js | 72 ++++++++++++++++++---- functions.js | 140 ++++++++++++++++++++++++++++-------------- objects.js | 107 +++++++++++++++++++++++--------- 5 files changed, 319 insertions(+), 108 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..573b9e4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/arrays.js b/arrays.js index 9a5827f..2b9723c 100644 --- a/arrays.js +++ b/arrays.js @@ -1,10 +1,13 @@ -// To help us use arrays with real world problems we are going to simulate a used car dealer that has 50 cars in their inventory. +// To help us use arrays with real world problems we are going to simulate a used car dealer that has 50 cars in their +// inventory. // Si aad u barato isticmaalka ARRAY, waxaan halkaan ku heynaa meesha gawaarida lagu gado oo 50 gaari heyso. -// The car dealer has all of their inventory housed in the array seen below. Scroll down past the data to find out how you can help the car dealer. +// The car dealer has all of their inventory housed in the array seen below. Scroll down past the data to find +// out how you can help the car dealer. -// Baabuurta oo dhan waxay ku jiraan array, hoos waxaa ku qoran babab aad ku caawin kartid ganacsigaan adigoo code isticmaalayo. +// Baabuurta oo dhan waxay ku jiraan array, hoos waxaa ku qoran babab aad ku caawin kartid ganacsigaan adigoo +// code isticmaalayo. let inventory = [ { id: 1, car_make: "Lincoln", car_model: "Navigator", car_year: 2009 }, @@ -61,7 +64,8 @@ let inventory = [ // Example Challenge: - // The dealer wants to know how many "Mazda" cars they have. / Waxay rabaan inay ogaadaan meeqo gaari oo "Mazda" ah ayaa yaalo. + // The dealer wants to know how many "Mazda" cars they have. / Waxay rabaan inay ogaadaan meeqo gaari oo "Mazda" ah + // ayaa yaalo. let mazdaCars = [] @@ -71,41 +75,102 @@ let inventory = [ } } - console.log("We have ", mazdaCars.length, "Mazda Cars") + // console.log("We have ", mazdaCars.length, "Mazda Cars") // ==== Challenge 1 ==== - // The dealer can't recall the information for a car with an id of 33 on his lot. Help the dealer find out which car has an id of 33 by logging the car's year, make, and model in the console log provided to you below: + // The dealer can't recall the information for a car with an id of 33 on his lot. Help the dealer find + // out which car has an id of 33 by logging the car's year, make, and model in the console log provided to you below: - // Ma xasuustaan ID-ga gaariga 33 aad, ka caawi inay ogaadaan ID-ga gaariga 33aad. Waa inaa soo bandhigto sanadka gaariga, noocuu yahay (make) iyo modelka gaariga. Habkaan usoo bandhig: + // Ma xasuustaan ID-ga gaariga 33 aad, ka caawi inay ogaadaan ID-ga gaariga 33aad. Waa inaa soo bandhigto + // sanadka gaariga, noocuu yahay (make) iyo modelka gaariga. Habkaan usoo bandhig: // console.log(`Car 33 is a *car year goes here* *car make goes here* *car model goes here*`); + console.log(`Car ${inventory[32].id} is a ${inventory[32].car_year} ${inventory[32].car_make} + ${inventory[32].car_model}`); + + - // ==== Challenge 2 ==== - // The dealer needs the information on the last car in their inventory. What is the make and model of the last car in the inventory? Log the make and model into the console. + // The dealer needs the information on the last car in their inventory. What is the make and model of the last car + // in the inventory? Log the make and model into the console. - // Waxay rabaan inay ogaadaan macluumaadka gaariga ugu dambeeyay. Waxaa ka mid ah inay noocuu yahay (make) iyo modelka gaariga ugu dambeeyay. + // Waxay rabaan inay ogaadaan macluumaadka gaariga ugu dambeeyay. Waxaa ka mid ah inay noocuu yahay (make) + // iyo modelka gaariga ugu dambeeyay. + let lastCar = [] + for(let i = 0; i < inventory.length; i++){ + if(inventory[i].car_year >= 2011){ + lastCar.push(inventory[i].car_make) + lastCar.push(inventory[i].car_model) + lastCar.push(inventory[i].car_year) + } + +} + +console.log(lastCar); - // ==== Challenge 3 ==== - // The marketing team wants the car models listed alphabetically on the website. Sort all the car model names into alphabetical order and log the results in the console + // The marketing team wants the car models listed alphabetically on the website. Sort all the car model + // names into alphabetical order and log the results in the console - // Dadka qaabilsan xayaysiinta ayaa rabo in gawaarida loo soo bandhigo xarfaha habkey iskugu xigaan (alphabetically) si ay website-ka u galiyaan. Magacyada gawaawida oo dhan isku habee si A-Z ah kadibna console.log ku samee. - + // Dadka qaabilsan xayaysiinta ayaa rabo in gawaarida loo soo bandhigo xarfaha habkey iskugu xigaan + //(alphabetically) si ay website-ka u galiyaan. Magacyada gawaawida oo dhan isku habee si A-Z ah kadibna + // console.log ku samee. + + let carModelsSort = [] + for(let i = 0; i < inventory.length; i++){ + carModelsSort.push(inventory[i].car_model) + } + + carModelsSort.sort() + console.log(carModelsSort); // ==== Challenge 4 ==== - // The accounting team needs all the years from every car on the lot. Create a new array from the dealer data containing only the car years and log the result in the console. + // The accounting team needs all the years from every car on the lot. Create a new array from + // the dealer data containing only the car years and log the result in the console. + + // Dadka qaabilsan xisaabinta ayaa rabo inay ogaadaan sanadyada gawaarida oo dhan. + // Array cusub samee, kadibna ku shub sanadyada gawaarida oo dhan kadibna console.log ku samee. - // Dadka qaabilsan xisaabinta ayaa rabo inay ogaadaan sanadyada gawaarida oo dhan. Array cusub samee, kadibna ku shub sanadyada gawaarida oo dhan kadibna console.log ku samee. + const allCarYears = [] + for(let i = 0; i < inventory.length; i++){ + allCarYears.push(inventory[i].car_year) + } + + console.log(allCarYears); // ==== Challenge 5 ==== - // The car lot manager needs to find out how many cars are older than the year 2000. Find out how many cars were made before the year 2000 by populating the array oldCars and logging it's length. + // The car lot manager needs to find out how many cars are older than the year 2000. Find + // out how many cars were made before the year 2000 by populating the array oldCars and + // logging it's length. - // Qofka maamulo ganacsiga ayaa rabo inuu ogaado inta gaari oo ka horeysay sanadkii 2000. Isticmaal array 'oldCars', kuna shub gawaarida ka horeysay 2000. Kadib console log ku samee. + // Qofka maamulo ganacsiga ayaa rabo inuu ogaado inta gaari oo ka horeysay + // sanadkii 2000. Isticmaal array 'oldCars', kuna shub gawaarida ka horeysay 2000. + // Kadib console log ku samee. +let olderCars = [] + + for(let i = 0; i < inventory.length; i++){ + if(inventory[i].car_year < 2000){ + olderCars.push(inventory[i].car_year) + olderCars.push(inventory[i].car_make) + olderCars.push(inventory[i].car_model) + } + } + + console.log(olderCars); + + // ==== Challenge 6 ==== - // A buyer is interested in seeing only BMW and Audi cars within the inventory. Return an array that only contains BMW and Audi cars. Once you have populated the BMWAndAudi array, use JSON.stringify() to show the results of the array in the console. + // A buyer is interested in seeing only BMW and Audi cars within the inventory. Return an array that only + // contains BMW and Audi cars. Once you have populated the BMWAndAudi array, use JSON.stringify() to + // show the results of the array in the console. - // Qof rabo inuu gaari gato ayaa rabo inuu ogaado inta BMW iyo Audi yaalo. Array 'BMWAndAudi' la dhaho ku shub dhamaan gawaarida BMW iyo Audi. Kadib adigoo isticmaalaayo JSON.stringify() console.log ku samee. + // Qof rabo inuu gaari gato ayaa rabo inuu ogaado inta BMW iyo Audi yaalo. Array 'BMWAndAudi' la + //dhaho ku shub dhamaan gawaarida BMW iyo Audi. Kadib adigoo isticmaalaayo JSON.stringify() console.log ku samee. + + let BMWAndAudi = inventory.filter(car => car.car_make === 'BMW' || car.car_make === 'Audi'); + console.log(JSON.stringify(BMWAndAudi)); + + diff --git a/basics.js b/basics.js index 8036dce..ddbdccb 100644 --- a/basics.js +++ b/basics.js @@ -8,21 +8,35 @@ Do the following: HINT: look up the Number method / Raadi Number Method wax la dhaho */ +const year = "1999" +let yearString = Number(year) +console.log(yearString); /* Task: Mood Checker Do the following: - 1. Write a script that prompts the user for their current mood. / Code qor u soo saaraayo qof meeshuu ku qori lahaa siduu dareemaayo - 2. If the user inputs happy, print 'Yay me too!' to the console, sad print 'Aw cheer up', / Haduu qofka galiyo 'happy', console log samee ah 'Yay me too', haduu 'sad' galiyo, 'Aw Cheer up' console ku qor. - 3. Else just print 'So moody!' / Hadii kale 'So Moody!' soo saar. + 1. Write a script that prompts the user for their current mood. / Code qor u soo saaraayo qof meeshuu ku qori lahaa siduu dareemaayo + 2. If the user inputs happy, print 'Yay me too!' to the console, sad print 'Aw cheer up', / Haduu qofka galiyo 'happy', console log samee ah 'Yay me too', haduu 'sad' galiyo, 'Aw Cheer up' console ku qor. + 3. Else just print 'So moody!' / Hadii kale 'So Moody!' soo saar. */ +let mood = prompt('what is your curent mood ') + +if(mood === 'happy'){ + console.log('Yay me too!'); + +} else if (mood === 'sad'){ +console.log('Aw cheer up'); +}else{ + console.log('So Moody!'); +} /* -Task: Odd or Even / kisi ama dhaban +/ kisi ama dhaban +Task: Odd or Even Use conditionals to check if a hardcoded number is odd or even, and then console.log the number is odd or even with the numbers value. @@ -35,7 +49,14 @@ var num = 16; // You can change this number! / Number-kaan ku bilow // write your conditions here / Code-kaada halkaan ku qor +if(num % 2 === 0){ + console.log(num + 'is even'); +}else{ + console.log(num + 'is odd'); +} + +console.log(num); /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task: FIZZBUZZ 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀*/ @@ -44,7 +65,8 @@ var num = 16; // You can change this number! / Number-kaan ku bilow You're about to do an assignment called "Fizz Buzz", which is one of the classic programming challenges. -"Fizz Buzz" waa xujo la siiyo markii dadka interview laga qaadaayo. Waa inuu programkaada soo saaro "Fizz", "Buzz" ama "FizzBuzz". +"Fizz Buzz" waa xujo la siiyo markii dadka interview laga qaadaayo. Waa inuu programkaada soo saaro "Fizz", "Buzz" +ama "FizzBuzz". Write a program that prints the numbers from 1 to 100. @@ -67,7 +89,7 @@ It is represented by the percentage sign (%) and it gives you the remainder left Waxaa jiro wax la yiraahdo "modulus" oo Javascript ku jiro, kaas oo uu kuu soo saaraayo haraaga labo number oo la isku dhuftay, sidaan ayuu u qoranyahay: - 6 % 3 +6 % 3 Equals zero. Because after dividing 6 by 3, there is zero leftover. Whereas: @@ -93,20 +115,46 @@ It's okay for it to be slow. */ +for(let i = 1; i <= 100; i++ ){ + if (i % 3 === 0 && i % 5 === 0) { + console.log('Fizbuzz'); + } else if(i % 3 === 0){ + console.log('fizz'); + }else if(i % 5 === 0) { + console.log('buzz'); + }else{ + console.log(i); + } +} /*💪💪💪💪💪💪💪💪💪💪 Stretch 💪💪💪💪💪💪💪💪💪💪*/ //Vowel Counter - How many vowels are there? /* Using the vowelCounter function below do the following: - 1. Receive a string as a parameter - 2. Count and return the number of vowels within that string. It should handle both capitalized and uncapitalized vowels. + 1. Receive a string as a parameter + 2. Count and return the number of vowels within that string. It should handle both capitalized and uncapitalized vowels. - HINT - you may need to study next week's content on arrays - HINT - try looking up the .includes() method + HINT - you may need to study next week's content on arrays + HINT - try looking up the .includes() method */ -function vowelCounter(/*add your code here*/) { - /*add your code here*/ +function vowelCounter(str) { + let count = 0; + const vowels = ["a", "e", "i", "o", "u" ]; + + for (let i = 0; i < str.length; i++) { + let elpha = str[i].toLowerCase(); + if (vowels.includes(elpha)) { + count++; + } + } + + return count; } + + + +console.log(vowelCounter("Hello, World!")); // Outputs: 3 +console.log(vowelCounter("Hello, teacher soo diyaar maha !")); // Outputs: 3 diff --git a/functions.js b/functions.js index e88aca4..e7aabfd 100644 --- a/functions.js +++ b/functions.js @@ -3,52 +3,100 @@ // Take the commented ES5 syntax and convert it to ES6 arrow Syntax -/* ------------- -function myFunction() { -console.log("Function was invoked!"); -}; -myFunction(); ----------------- -let anotherFunction = function (param) { - return param; -}; - -anotherFunction("Example"); - ---------------- - -let add = function (param1, param2) { - return param1 + param2; -}; - -add(1,2); - -*/ - - - -/*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task: Rock, Paper, Scissors - Let's play against the computer! 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀*/ - -/* -Create a global variable that randomly generates the computer's choice -Use Math.random to determine the computers choice (Math.random gives a random number between 0 and 1) -HINT: While you can complete this with only conditionals based on strings, it may help to equate choice to a number when using Math.random() -Use the game function below to do the following: - 1. Receive 2 parameters the user's choice and the computer's choice - 2. Return whether the user won, lost, or tied based on these rules of the game described below - the strings returned need to match the strings below exactly. - - win should return "you win!" - - lose should return "you lose!" - - tie should return "it's a tie" - - RULES OF THE GAME: Scissors beats Paper | Paper beats Rock | Rock beats Scissors | Or there's a tie - - HINT: Remember that the order in which we pass in our arguments matters when it comes to parameters -*/ - -function game(user, computer){ - /*add your code here*/ -} +let myFunction = () => { + console.log("Function was invoked!"); + }; + + myFunction(); + + let anotherFunction = (param) => { + return param; + }; + + anotherFunction("Example"); + + + + let add = (param1, param2) => { + return param1 + param2; + }; + + add(1,2); + + + + + + /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task: Rock, Paper, Scissors - Let's play against the computer! 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀*/ + + /* + Create a global variable that randomly generates the computer's choice + Use Math.random to determine the computers choice (Math.random gives a random number between 0 and 1) + HINT: While you can complete this with only conditionals based on strings, it may help to equate + choice to a number when using Math.random() + Use the game function below to do the following: + 1. Receive 2 parameters the user's choice and the computer's choice + 2. Return whether the user won, lost, or tied based on these rules of the game described below - + the strings returned need to match the strings below exactly. + - win should return "you win!" + - lose should return "you lose!" + - tie should return "it's a tie" + + RULES OF THE GAME: Scissors beats Paper | Paper beats Rock | Rock beats Scissors | + Or there's a tie + + HINT: Remember that the order in which we pass in our arguments matters when + it comes to parameters + // */ + + // function game(user, computer){ + // /*add your code here*/ + // } + + let computerChoice; + + computerChoice = Math.random(); + + if (computerChoice < 0.33) { + computerChoice = "rock"; + } else if (computerChoice < 0.67) { + computerChoice = "paper"; + } else { + computerChoice = "scissors"; + } + + // function to play the game + function game(userChoice, computerChoice) { + if (userChoice === "rock") { + if (computerChoice === "rock") { + return "it's a tie"; + } else if (computerChoice === "paper") { + return "you lose!"; + } else { + return "you win!"; + } + } else if (userChoice === "paper") { + if (computerChoice === "rock") { + return "you win!"; + } else if (computerChoice === "paper") { + return "it's a tie"; + } else { + return "you lose!"; + } + } else { + if (computerChoice === "rock") { + return "you lose!"; + } else if (computerChoice === "paper") { + return "you win!"; + } else { + return "it's a tie"; + } + } + } + + let result = game("rock", computerChoice); + + console.log(result); \ No newline at end of file diff --git a/objects.js b/objects.js index 9640e8c..7c589a8 100644 --- a/objects.js +++ b/objects.js @@ -11,34 +11,81 @@ // Example format of an intern object: 1, examples@you.edu, Example, F const example = { - id: 0, - name: "Example", - email: "examples@you.edu", - gender: "F", + id: 0, + name: "Example", + email: "examples@you.edu", + gender: "F", +} + +// Write your intern objects here: + +const mitzi = { + id: 1, + name: 'mitzi', + email: 'mmelloy0@psu.ed', + gender: 'F', +} + +const kennan = { + id: 2, + name: 'kennan', + email: 'kdiben1@tinypic.com', + gender: 'm', + speak: function() { + return `Hello, my name is ${this.name}.`; + } +} + +const Keven = { + id: 3, + name: 'keven', + email: 'kmummery2@wikimedia.org ', + gender: 'm', +} + +const Gannie = { + id: 4, + name: 'Gannie', + email: 'gmartinson3@illinois.edu', + gender: 'm', +} + + +const Antonietta = { + id: 5, + name: 'Antonietta', + email: 'adaine5@samsung.com', + gender: 'F', + multiplyNums: function(num1, num2) { + return num1 * num2; } - - // Write your intern objects here: - - - // ==== Challenge 2: Reading Object Data ==== - // Once your objects are created, log out the following requests from HR into the console: - - // Mitzi's name - - // Kennan's ID - - // Keven's email - - // Gannie's name - - // Antonietta's Gender - - // ==== Challenge 3: Object Methods ==== - // Give Kennan the ability to say "Hello, my name is Kennan!" Use the console.log provided as a hint. - // console.log(kennan.speak()); - - // Antonietta loves math, give her the ability to multiply two numbers together and return the product. Use the console.log provided as a hint. - //console.log(antonietta.multiplyNums(3,4)); - - // === Great work! === Head over to the the arrays.js. You may come back and attempt the Stretch Challenge once you have completed the challenges in arrays.js and function-conversion.js. - \ No newline at end of file +} + +// ==== Challenge 2: Reading Object Data ==== +// Once your objects are created, log out the following requests from HR into the console: + +// Mitzi's name +console.log(mitzi.name); +// Kennan's ID +console.log(kennan.id); +// Keven's email +console.log(Keven.email); +// Gannie's name +console.log(Gannie.name); +// Antonietta's Gender +console.log(Antonietta.gender); +// ==== Challenge 3: Object Methods ==== +// Give Kennan the ability to say "Hello, my name is Kennan!" Use the console.log provided +// as a hint. +// console.log(kennan.speak()); + console.log(kennan.speak()); + +// Antonietta loves math, give her the ability to multiply two numbers together and return the +// product. Use the console.log provided as a hint. +//console.log(antonietta.multiplyNums(3,4)); + +console.log(Antonietta.multiplyNums(3,4)); + +// === Great work! === Head over to the the arrays.js. You may come back and attempt +//the Stretch Challenge once you have completed the challenges in arrays.js and +//function-conversion.js. \ No newline at end of file