Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
js1-week3-Hani
  • Loading branch information
HaniBadran authored Dec 14, 2019
commit 681e0b6038660b5e8e99601d74d1f67c958ae4b3
12 changes: 12 additions & 0 deletions Week3/1-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';


function giveCompliment(name) {
let compliments = ['Great!', 'Awesome!', 'fabulous!', 'elegant', 'magnificent! ', 'super!', 'superior', 'Doing well!', 'Excelent!', 'Nice!' ];
return `You are ${compliments[Math.floor(Math.random() * 10)]} ${name}!`
}

console.log(giveCompliment('Hani'));
console.log(giveCompliment('Hani'));
console.log(giveCompliment('Hani'));
console.log(giveCompliment('Hani'));
9 changes: 9 additions & 0 deletions Week3/2-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';


let calculateDogAge = age => age * 7;

console.log(calculateDogAge(12));
console.log(calculateDogAge(1));
console.log(calculateDogAge(0.5));
console.log(calculateDogAge(7));
13 changes: 13 additions & 0 deletions Week3/3-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';


let jobs = ['Teacher', 'Web developer', 'Salesman', 'Baker', 'Cashier']
let locations = ['Amsterdam', 'London', 'Baghdad', 'Delhi', 'Beirut']
let partnerNames = ['Margaret', 'Jane', 'Anna', 'Sabha', 'Moza']
let numChildren = [0 , 10, 1, 2 , 3]

function tellFortune(job, locations, partnerNames, numChildren) {
return `You will be a ${job[Math.floor(Math.random() * 5)]} in ${locations[Math.floor(Math.random() * 5)]}, and married to ${partnerNames[Math.floor(Math.random() * 5)]} with ${numChildren[Math.floor(Math.random() * 5)]} kids.`
}

console.log(tellFortune(jobs, locations, partnerNames, numChildren))
21 changes: 21 additions & 0 deletions Week3/4-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';


let shoppings = ['milk', 'banana' ]

function addToShoppingCart(item) {
if (shoppings.length <= 2){
shoppings.push(item)
return console.log(`You bought ${shoppings}!`);
} else if (shoppings.length <= 3){
shoppings.shift();
shoppings.push(item)
return console.log(`You bought ${shoppings}!`)
}

}

addToShoppingCart('cheese');
addToShoppingCart('Chocolate');
addToShoppingCart('Apples');
addToShoppingCart('yoghurt');
80 changes: 80 additions & 0 deletions Week3/PROJECT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';


let num = 4444444444444444 ; // not valid
let num1 = 1000111111111110 ; //not valid
let num2 = 9999999999999991 ; //not valid
let num3 = 8899777755550000 ; //vaild
let num4 = 5554446669991666 ; //valid


checkCard(num);
checkCard(num1);
checkCard(num2);
checkCard(num3);
checkCard(num4)

function checkCard(creditNum){
// it must be 16 numbers
let length ;

// change the number to a string so it has length property
let creditNumString = creditNum.toString();

//change the number to an array that so it has one string of the number
let creditNumArr = creditNumString.split(',');

// check if the number has 16 digits
if (creditNumString.length< 16 || creditNumString.length > 16) {
length = false;

} else {
length = true;
}

// The final digit must be even
let finalIsEven;
if (creditNumString[15] % 2 === 0) {
finalIsEven = true;
} else {
finalIsEven = false;
}

// The sum of all the digits must be greater than 16
let sumHigherThan16

// to be able to sum the numbers, must to make it an arry
let creditNumArrOfStrings = [];
for (var i = 0; i < creditNumString.length; i++) {
creditNumArrOfStrings.push(parseFloat(creditNumString[i]));
}

// this calculates the numbers
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sum = creditNumArrOfStrings.reduce(reducer);

if (sum <= 16) {
sumHigherThan16 = false;
} else {
sumHigherThan16 = true;}


// the digits cannot be the same
let notTheSameNum ;

// make it an arry
const allEqual = creditNumArrOfStrings => creditNumArrOfStrings.every( v => v === creditNumArrOfStrings[0] );
if (allEqual(creditNumArrOfStrings)) {
notTheSameNum = false;
} else {
notTheSameNum = true;
}

if (length && notTheSameNum && finalIsEven && sumHigherThan16) {
console.log('Valid credit card Number');
} else { console.log(`${creditNum} is not valid. Please enter another number`);
}

}