Merge 9c955ff63c
into 26b7c59eec
commit
fab388a294
@ -1 +1,155 @@
|
||||
// this is your main.js script
|
||||
// Exercise 01
|
||||
|
||||
// Declare a variable named challenge and assign it an initial value
|
||||
let challenge = '30 Days Of JavaScript';
|
||||
|
||||
// Print the string on the browser console
|
||||
console.log(challenge);
|
||||
|
||||
// Print the length of the string
|
||||
console.log(challenge.length);
|
||||
|
||||
// Change all characters to capital letters
|
||||
console.log(challenge.toUpperCase());
|
||||
|
||||
// Change all characters to lowercase letters
|
||||
console.log(challenge.toLowerCase());
|
||||
|
||||
// Slice out the first word of the string
|
||||
console.log(challenge.substr(0, 2)); // Assuming you want the first word "30"
|
||||
|
||||
// Slice out the phrase "Days Of JavaScript"
|
||||
console.log(challenge.substring(3)); // Assuming you want to exclude the initial "30 "
|
||||
|
||||
// Check if the string contains the word "Script"
|
||||
console.log(challenge.includes('Script'));
|
||||
|
||||
// Split the string into an array
|
||||
console.log(challenge.split());
|
||||
|
||||
// Split the string at the space
|
||||
console.log(challenge.split(' '));
|
||||
|
||||
// Split the string at the comma and change it to an array
|
||||
let companies = 'Facebook, Google, Microsoft, Apple, IBM, Oracle, Amazon';
|
||||
console.log(companies.split(', '));
|
||||
|
||||
// Change "30 Days Of JavaScript" to "30 Days Of Python"
|
||||
console.log(challenge.replace('JavaScript', 'Python'));
|
||||
|
||||
// Character at index 15
|
||||
console.log(challenge.charAt(15));
|
||||
|
||||
// Character code of J
|
||||
console.log(challenge.charCodeAt(challenge.indexOf('J')));
|
||||
|
||||
// Position of first occurrence of 'a'
|
||||
console.log(challenge.indexOf('a'));
|
||||
|
||||
// Position of last occurrence of 'a'
|
||||
console.log(challenge.lastIndexOf('a'));
|
||||
|
||||
// Position of first occurrence of 'because' in a sentence
|
||||
let sentence = 'You cannot end a sentence with because because because is a conjunction';
|
||||
console.log(sentence.indexOf('because'));
|
||||
|
||||
// Position of last occurrence of 'because' in a sentence
|
||||
console.log(sentence.lastIndexOf('because'));
|
||||
|
||||
// Position of first occurrence of 'because' using search
|
||||
console.log(sentence.search('because'));
|
||||
|
||||
// Remove trailing whitespace
|
||||
console.log(challenge.trim());
|
||||
|
||||
// Check if it starts with '30 Days Of JavaScript'
|
||||
console.log(challenge.startsWith('30 Days Of JavaScript'));
|
||||
|
||||
// Check if it ends with '30 Days Of JavaScript'
|
||||
console.log(challenge.endsWith('30 Days Of JavaScript'));
|
||||
|
||||
// Find all occurrences of 'a' using match
|
||||
console.log(challenge.match(/a/g));
|
||||
|
||||
// Concatenate strings
|
||||
console.log('30 Days of '.concat('JavaScript'));
|
||||
|
||||
// Repeat the string
|
||||
console.log(challenge.repeat(2));
|
||||
|
||||
//Excercise JavaScript level 02
|
||||
|
||||
// Exercise 1
|
||||
console.log("The quote 'There is no exercise better for the heart than reaching down and lifting people up.' by John Holmes teaches us to help one another.");
|
||||
|
||||
// Exercise 2
|
||||
console.log("\"Love is not patronizing and charity isn't about pity, it is about love. Charity and love are the same -- with charity you give love, so don't just give money but reach out your hand instead.\"");
|
||||
|
||||
// Exercise 3
|
||||
if (typeof '10' !== 'number') {
|
||||
'10' = parseInt('10');
|
||||
}
|
||||
|
||||
// Exercise 4
|
||||
if (parseFloat('9.8') !== 10) {
|
||||
'9.8' = parseFloat('10');
|
||||
}
|
||||
|
||||
// Exercise 5
|
||||
const python = 'python';
|
||||
const jargon = 'jargon';
|
||||
console.log('on' in python && 'on' in jargon);
|
||||
|
||||
// Exercise 6
|
||||
const sentence = 'I hope this course is not full of jargon.';
|
||||
console.log(sentence.includes('jargon'));
|
||||
|
||||
// Exercise 7
|
||||
const randomNumber1 = Math.floor(Math.random() * 101);
|
||||
console.log(randomNumber1);
|
||||
|
||||
// Exercise 8
|
||||
const randomNumber2 = Math.floor(Math.random() * 51) + 50;
|
||||
console.log(randomNumber2);
|
||||
|
||||
// Exercise 9
|
||||
const randomNumber3 = Math.floor(Math.random() * 256);
|
||||
console.log(randomNumber3);
|
||||
|
||||
// Exercise 10
|
||||
const jsString = 'JavaScript';
|
||||
const randomIndex = Math.floor(Math.random() * jsString.length);
|
||||
console.log(jsString[randomIndex]);
|
||||
|
||||
// Exercise 11
|
||||
console.log("1 1 1 1 1");
|
||||
console.log("2 1 2 4 8");
|
||||
console.log("3 1 3 9 27");
|
||||
console.log("4 1 4 16 64");
|
||||
console.log("5 1 5 25 125");
|
||||
|
||||
// Exercise 12
|
||||
const sentence2 = 'You cannot end a sentence with because because because is a conjunction';
|
||||
const extractedPhrase = sentence2.substr(30, 23);
|
||||
console.log(extractedPhrase);
|
||||
|
||||
//Exercise level 03
|
||||
|
||||
const sentence1 = 'Love is the best thing in this world. Some found their love and some are still looking for their love.';
|
||||
const countLove1 = (sentence1.match(/love/gi) || []).length;
|
||||
console.log(countLove1); // Output: 3
|
||||
|
||||
//
|
||||
const sentence2 = 'You cannot end a sentence with because because because is a conjunction';
|
||||
const countBecause = (sentence2.match(/\bbecause\b/gi) || []).length;
|
||||
console.log(countBecause); // Output: 3
|
||||
|
||||
//
|
||||
const text4 = 'He earns 5000 euro from salary per month, 10000 euro annual bonus, 15000 euro online courses per month.';
|
||||
|
||||
// Extract numbers using regular expression and sum them up
|
||||
const numbers = text4.match(/\d+/g).map(Number);
|
||||
const totalAnnualIncome = numbers.reduce((total, num) => total + num, 0);
|
||||
|
||||
console.log(totalAnnualIncome); // Output: 30000
|
||||
|
@ -1 +1,280 @@
|
||||
// this is your main.js script
|
||||
|
||||
//exercice level 1
|
||||
//ex 01
|
||||
let firstName = 'Belaid';
|
||||
let lastName = 'DALI OMAR';
|
||||
let country = 'Algeria';
|
||||
let city = 'Tizi Ouzou';
|
||||
let age = 30;
|
||||
let isMarried = false;
|
||||
let year = 1993;
|
||||
|
||||
console.log(typeof(firstName)); //string
|
||||
|
||||
console.log(typeof(lastName)); //string
|
||||
|
||||
console.log(typeof(country)); //string
|
||||
|
||||
console.log(typeof(city)); //string
|
||||
|
||||
console.log(typeof(age)); //number
|
||||
|
||||
console.log(typeof(isMarried)); //boolean
|
||||
|
||||
console.log(typeof(year)); //number
|
||||
|
||||
//exo 02
|
||||
console.log('10' == 10); //true
|
||||
|
||||
//exo 03
|
||||
let l = parseInt('9.8');
|
||||
console.log(a == 10); //False
|
||||
|
||||
//exo 04
|
||||
//Truthy values:
|
||||
|
||||
let truthyValue = 42;
|
||||
let truthyString = "Hello, world!";
|
||||
let truthyArray = [1, 2, 3];
|
||||
|
||||
// Falsy values:
|
||||
|
||||
let falsyValue = 0;
|
||||
let falsyString = "";
|
||||
let falsyNull = null;
|
||||
|
||||
//exo 05
|
||||
console.log(4>3); //true
|
||||
console.log(4>=3); //true
|
||||
console.log(4<3); //false
|
||||
console.log(4 == 4); //true
|
||||
console.log(4 === 4); //true
|
||||
console.log(4 != 4); //False
|
||||
console.log(4 !== 4); //false
|
||||
console.log(4 != '4'); //false
|
||||
console.log(4 == '4'); //true
|
||||
console.log(4 === '4'); //false
|
||||
|
||||
const pythonLength = "python".length;
|
||||
const jargonLength = "jargon".length;
|
||||
|
||||
const isFalsyComparison = pythonLength === jargonLength ? "Falsy" : "Not Falsy";
|
||||
|
||||
console.log(isFalsyComparison);
|
||||
|
||||
//exo 06
|
||||
|
||||
console.log(4 > 3 && 10 < 12); //true
|
||||
console.log(4 > 3 && 10 > 12); //false
|
||||
console.log(4 > 3 || 10 < 12); //true
|
||||
console.log(4 > 3 || 10 > 12); //true
|
||||
console.log(!(4 > 3)); //false
|
||||
console.log(!(4 < 3)); //true
|
||||
console.log(!(false)); //true
|
||||
console.log(!(4 > 3 && 10 < 12)); //false
|
||||
console.log(!(4 > 3 && 10 > 12)); //true
|
||||
console.log(!(4 === '4')); //true
|
||||
|
||||
const str1 = 'python';
|
||||
const str2 = 'dragon';
|
||||
const isFalsyVal = str1.includes('on') || str2.includes('on') ? true : false;
|
||||
console.log(isFalsyVal); //true
|
||||
|
||||
//exo 07
|
||||
|
||||
const time = new Date();
|
||||
console.log(time); //Wed Aug 23 2023 10:39:33 GMT+0100 (Central European Standard Time)
|
||||
console.log(time.getFullYear()); // 2023
|
||||
console.log(time.getMonth() + 1); // 7
|
||||
console.log(time.getDate()); // 23
|
||||
console.log(time.getDay()); // 3
|
||||
console.log(time.getHours()); // 10
|
||||
console.log(time.getMinutes()); // 39
|
||||
console.log(time.getTime()); // 1692783573531 seconds since January 1, 1970 00:00:00 GMT to 10:39:33 GMT
|
||||
|
||||
//Exercice level 02
|
||||
|
||||
//exo 01
|
||||
const base = parseFloat(prompt('Enter base:'));
|
||||
const h = parseFloat(prompt('Enter Height:'));
|
||||
let area = 0.5*base*h;
|
||||
console.log(`the area is ${area}`); // 100
|
||||
|
||||
// exo 02
|
||||
|
||||
let a = parseFloat(prompt('Enter side a:'));
|
||||
let b = parseFloat(prompt('Enter side b:'));
|
||||
let c = parseFloat(prompt('Enter side c:'));
|
||||
let p = a+b+c;
|
||||
console.log(`the perimeter is ${p}`); //12
|
||||
|
||||
//exo 03
|
||||
|
||||
let length = parseFloat(prompt('Enter L: '));
|
||||
let width = parseFloat(prompt('Enter W: '));
|
||||
let areas = length * width;
|
||||
let perimeter = 2*(length+width);
|
||||
console.log(`the perimeter is ${perimeter}`); //
|
||||
console.log(`the area is ${areas}`); //
|
||||
|
||||
//exo 04
|
||||
|
||||
let r = parseFloat(prompt('Enter L: '));
|
||||
const Pi = 3.14;
|
||||
let ar = r * r * Pi;
|
||||
let per= 2*Pi*r;
|
||||
console.log(`the perimeter is ${per}`); //
|
||||
console.log(`the area is ${ar}`); //
|
||||
|
||||
//ex 05
|
||||
|
||||
// Equation: y = 2x - 2
|
||||
|
||||
// Slope (m) is the coefficient of x
|
||||
const slope = 2;
|
||||
|
||||
// x-intercept: When y = 0
|
||||
// 0 = 2x - 2
|
||||
// 2x = 2
|
||||
// x = 1
|
||||
const xIntercept = 1;
|
||||
|
||||
// y-intercept: When x = 0
|
||||
const yIntercept = -2;
|
||||
|
||||
console.log(`Slope (m): ${slope}`);
|
||||
console.log(`x-intercept: (${xIntercept}, 0)`);//(1,0)
|
||||
console.log(`y-intercept: (0, ${yIntercept})`);//(0,-2)
|
||||
|
||||
//EXO 06
|
||||
|
||||
// Points
|
||||
const x1 = 2;
|
||||
const y1 = 2;
|
||||
const x2 = 6;
|
||||
const y2 = 10;
|
||||
|
||||
// Calculate the slope
|
||||
const slopes = (y2 - y1) / (x2 - x1);
|
||||
|
||||
console.log(`The slope between (${x1}, ${y1}) and (${x2}, ${y2}) is ${slopes}`);//2
|
||||
|
||||
//exo 07
|
||||
|
||||
// Equation: y = 2x - 2
|
||||
|
||||
// Given points
|
||||
const x1 = 2;
|
||||
const y1 = 2;
|
||||
const x2 = 6;
|
||||
const y2 = 10;
|
||||
|
||||
// Calculate the slope for the equation y = 2x - 2
|
||||
const equationSlope = 2;
|
||||
|
||||
// Calculate the slope between the two points
|
||||
const pointSlope = (y2 - y1) / (x2 - x1);
|
||||
|
||||
// Compare the slopes using ternary operator
|
||||
const comparisonResult = equationSlope === pointSlope ? "Equal" : "Not Equal";
|
||||
|
||||
console.log(`Slope of y = 2x - 2: ${equationSlope}`);//Slope of y = 2x -2: 2
|
||||
console.log(`Slope between (${x1}, ${y1}) and (${x2}, ${y2}): ${pointSlope}`);//slope between (2,2) and (6,10): 2
|
||||
console.log(`Comparison result: Slopes are ${comparisonResult}`);// comparison result: Slopes are Equal
|
||||
|
||||
//Exo 08
|
||||
|
||||
// Equation: y = x^2 + 6x + 9
|
||||
|
||||
// Given equation coefficients
|
||||
const a = 1;
|
||||
const b = 6;
|
||||
const c = 9;
|
||||
|
||||
// Calculate the discriminant
|
||||
const discriminant = Math.sqrt(b * b - 4 * a * c);
|
||||
|
||||
// Calculate the two possible x values when y is 0
|
||||
const x1 = (-b + discriminant) / (2 * a);
|
||||
const x2 = (-b - discriminant) / (2 * a);
|
||||
|
||||
console.log(`x1: ${x1}`);//-3
|
||||
console.log(`x2: ${x2}`);//-3
|
||||
|
||||
//exo 09
|
||||
|
||||
let hours = parseFloat(prompt('Enter Hour'));
|
||||
let rateHr = parseFloat(prompt('Enter rate per hour'));
|
||||
const salary = hours * rateHr;
|
||||
console.log(`Your weekly earning is ${salary}`);//Your weekly earning is 1120
|
||||
|
||||
//exo 10
|
||||
|
||||
const name = 'Belaid';
|
||||
let L = name.length;
|
||||
(L > 7)
|
||||
? console.log('my name is long')
|
||||
: console.log('my name is short')
|
||||
//my name is short
|
||||
|
||||
//exo 11
|
||||
|
||||
let firstName = 'Belaid';
|
||||
let lastName = 'DALI OMAR';
|
||||
(length.firstName > length.lastName)
|
||||
? console.log(`my first name, ${firstName} is longer than my family name, ${lastName}`)
|
||||
: console.log(`my last name, ${lastName} is longer than my first name, ${firstName}`)
|
||||
//my last name, DALI OMAR is longer than my first name, Belaid
|
||||
|
||||
//exo 12
|
||||
|
||||
let myAge = 30;
|
||||
let yourAge = 32;
|
||||
console.log(`you are ${yourAge - myAge} years older than me`);
|
||||
// you are 2 years older than me
|
||||
|
||||
//exo 13
|
||||
|
||||
let birthYear = parseInt(prompt('Enter your birth year'));
|
||||
let ages = 2023 - birthYear;
|
||||
(ages>18)
|
||||
? console.log(`you are ${ages}. You are old enough to drive`)
|
||||
: console.log(`you are ${ages}. You will not be allowed to drive`);
|
||||
//you are 29. You are old enough to drive
|
||||
|
||||
//exo 14
|
||||
|
||||
let yrs = parseInt(prompt('Enter number of years you live'));
|
||||
const live = new Date();
|
||||
console.log(`you lived ${live.getTime()} seconds`);
|
||||
//you lived 1692797539157 seconds
|
||||
|
||||
//exo 15
|
||||
|
||||
const now = new Date();
|
||||
const years = now.getFullYear(); // return year
|
||||
const month = now.getMonth() + 1; // return month(0 - 11)
|
||||
const date = now.getDate(); // return date (1 - 31)
|
||||
const Hours = now.getHours(); // return number (0 - 23)
|
||||
const minutes = now.getMinutes(); // return number (0 -59)
|
||||
|
||||
console.log(`${years}-${month}-${date} ${Hours}:${minutes}`);
|
||||
//2023-8-23 14:38
|
||||
console.log(`${date}-${month}-${years} ${Hours}:${minutes}`);
|
||||
//23-8-2023 14:38
|
||||
console.log(`${date}/${month}/${years} ${Hours}:${minutes}`);
|
||||
// 23/8/2023 14:38
|
||||
|
||||
//exercise level 3
|
||||
|
||||
//exo 01
|
||||
|
||||
|
||||
const months = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const hourss = String(date.getHours()).padStart(2, '0');
|
||||
const Minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
|
||||
|
||||
console.log(`${years}-${months}-${day} ${hourss}:${Minutes}`);
|
@ -1,3 +1,139 @@
|
||||
// this is your main.js script
|
||||
|
||||
alert('Open the browser console whenever you work on JavaScript')
|
||||
|
||||
//Exo level 1
|
||||
|
||||
//exo 01
|
||||
let age = prompt('Enter your age');
|
||||
if(age>=18){
|
||||
"you are old enough to drive";
|
||||
} else {
|
||||
`you are left with ${18-age} years to drive`;
|
||||
}
|
||||
|
||||
//exo 02
|
||||
|
||||
let myAge = prompt('Enter your age');
|
||||
const yourAge = 45;
|
||||
if(myAge>yourAge){
|
||||
`Um ${myAge-yourAge} years oder than you`;
|
||||
} else {
|
||||
`you are ${yourAge-myAge} years older than me`;
|
||||
}
|
||||
|
||||
//exo 03
|
||||
|
||||
let a = parseInt(prompt('Enter a number'));
|
||||
let b= parseInt(prompt('Enter another number'));
|
||||
(a>b) ? console.log('a is greater than b') : console.log('b is greater than a');
|
||||
|
||||
//exo 04
|
||||
|
||||
let c = parseInt(prompt('Enter a number'));
|
||||
if(c%2==0){
|
||||
console.log(`${c} is even`);
|
||||
} else {
|
||||
console.log(`${c} is odd`);
|
||||
}
|
||||
|
||||
//exo level 2
|
||||
|
||||
let score = parseInt(prompt('Give your score:'));
|
||||
|
||||
let grade;
|
||||
|
||||
switch (true) {
|
||||
case (score >= 80 && score <= 100):
|
||||
grade = "A";
|
||||
break;
|
||||
case (score >= 70 && score < 80):
|
||||
grade = "B";
|
||||
break;
|
||||
case (score >= 60 && score < 70):
|
||||
grade = "C";
|
||||
break;
|
||||
case (score >= 50 && score < 60):
|
||||
grade = "D";
|
||||
break;
|
||||
case (score >= 0 && score < 50):
|
||||
grade = "F";
|
||||
break;
|
||||
default:
|
||||
grade = "Failed";
|
||||
}
|
||||
|
||||
console.log(`Your grade is ${grade}`);
|
||||
|
||||
//exo 02
|
||||
|
||||
let monthInput = prompt('Enter a month:');
|
||||
let month = monthInput.toLowerCase();
|
||||
let season;
|
||||
switch (true) {
|
||||
case month=="september" || month=="october" || month=="november" :
|
||||
season = "Autumn";
|
||||
break;
|
||||
case month=="decembre" || month=="january" || month=="february" :
|
||||
season = "Winter";
|
||||
break;
|
||||
case month=="march" || month=="april" || month=="may" :
|
||||
season = "Spring";
|
||||
break;
|
||||
case month=="june" || month=="july" || month=="august":
|
||||
season = "Summer";
|
||||
break;
|
||||
default :
|
||||
season = "";
|
||||
}
|
||||
|
||||
//exo 03
|
||||
|
||||
let dayInput = prompt("What is the day today?");
|
||||
let day = dayInput.toLowerCase();
|
||||
|
||||
if (day === "saturday" || day === "sunday") {
|
||||
console.log(`${day} is a weekend.`);
|
||||
} else if (day === "monday" || day === "tuesday" || day === "wednesday" || day === "thursday" || day === "friday") {
|
||||
console.log(`${day} is a working day.`);
|
||||
} else {
|
||||
console.log("Invalid day.");
|
||||
}
|
||||
|
||||
//exo level 3
|
||||
|
||||
//exo 01
|
||||
|
||||
let monthIn = prompt("Enter a month");
|
||||
let months = monthIn.toLowerCase();
|
||||
let days;
|
||||
|
||||
switch (months) {
|
||||
case "january":
|
||||
case "march":
|
||||
case "july":
|
||||
case "august":
|
||||
case "october":
|
||||
case "december":
|
||||
days = 31;
|
||||
break;
|
||||
case "april":
|
||||
case "june":
|
||||
case "september":
|
||||
case "november":
|
||||
days = 30;
|
||||
break;
|
||||
case "february":
|
||||
let yearInput = parseInt(prompt("Enter a year: "));
|
||||
let isLeapYear = (yearInput % 4 === 0 && yearInput % 100 !== 0) || (yearInput % 4 === 0);
|
||||
days = isLeapYear ? 29 : 28;
|
||||
break;
|
||||
default:
|
||||
console.error("Invalid month");
|
||||
break;
|
||||
}
|
||||
|
||||
if (days !== undefined) {
|
||||
|
||||
console.log(`${months} has ${days}`);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
const webTechs = [
|
||||
'HTML', 'CSS', 'JavaScript', 'React', 'Node',
|
||||
'PHP', 'EastJS', 'SQL', 'SASS', 'AngolaJS'
|
||||
]
|
||||
|
||||
const isSass = webTechs.includes('SASS');
|
||||
if (isSass) {
|
||||
console.log('SASS is a CSS preprocessor');
|
||||
} else {
|
||||
webTechs.push('SASS');
|
||||
console.log(webTechs);
|
||||
}
|
||||
|
||||
//exo 6
|
||||
const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']
|
||||
const backEnd = ['Node','Express', 'MongoDB']
|
||||
const fullStack = frontEnd.concat(backEnd);
|
||||
|
||||
console.log(fullStack);
|
Loading…
Reference in new issue