Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 0dad1a2

Browse files
authored
Add files via upload
1 parent ffd6f6f commit 0dad1a2

10 files changed

Lines changed: 273 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Write a function named addToShoppingCart.
3+
4+
It takes in 1 argument: a grocery item (string)
5+
Add grocery item to array. If the amount of items is more than 3 remove the first one in the array
6+
Return a string: "You bought [LIST_OF_GROCERY_ITEMS]!"
7+
8+
Create an array with 2 predefined strings: "bananas" and "milk"
9+
Call the function 3 times, each time with a different string as the argument.
10+
*/
11+
let groceryList = ["bananas", "Milk"];
12+
13+
function addToShoppingCart(groceryItem){
14+
groceryList.push(groceryItem);
15+
if(groceryList.length > 3)
16+
groceryList.shift();
17+
return "You bought " + groceryList.join(", ") + ".";
18+
}
19+
20+
function main(){
21+
console.log(addToShoppingCart("wine"));
22+
console.log(addToShoppingCart("cookies"));
23+
console.log(addToShoppingCart("oranges"));
24+
}
25+
26+
main();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Credit Card Validator</title>
7+
<link rel="stylesheet" href="creditCard.css">
8+
</head>
9+
<body>
10+
<!-- LEFT COLUMN-->
11+
<div class="leftSide">&nbsp</div>
12+
13+
<!-- CENTER COLUMN-->
14+
<div class="centerSide">
15+
<h3>CREDIT CARD VALIDATOR</h3>
16+
<input class="creditCardText" type="text" placeholder="Type your credit card number here...">
17+
<input class="validador" type="button" value="Validate">
18+
<img src="images/lock.jpg" alt="Smiley face">
19+
<p class="accessResult">&nbsp</p>
20+
</div>
21+
22+
<!-- RIGHT COLUMN-->
23+
<div class="rightSide">&nbsp</div>
24+
<script src="creditCard.js" charset="utf-8"></script>
25+
</body>
26+
</html>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
* {
2+
padding: 0px;
3+
margin: 0px;
4+
box-sizing: border-box;
5+
}
6+
7+
body div {
8+
height: 100vh;
9+
float: left;
10+
display: flex;
11+
flex-flow: column wrap;
12+
align-items: center;
13+
color: white;
14+
}
15+
16+
h3 {
17+
margin-top: 10px;
18+
}
19+
20+
input {
21+
width: 70%;
22+
margin-top: 10px;
23+
margin-bottom: 10px;
24+
}
25+
26+
img {
27+
width: 80px;
28+
height: 80px;
29+
margin-bottom: 30px;
30+
}
31+
32+
.leftSide {
33+
width: 20%;
34+
background: rgb(44, 43, 43);
35+
}
36+
37+
.centerSide {
38+
width: 60%;
39+
background: rgb(0, 0, 0);
40+
}
41+
42+
.rightSide {
43+
width: 20%;
44+
background: rgb(44, 43, 43);
45+
}
46+
47+
.validador {
48+
width: 30%;
49+
margin-top: 10px;
50+
margin-bottom: 30px;
51+
}
52+
53+
.accessResult {
54+
color: white;
55+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const creditCardValue = document.querySelector(".creditCardText");
2+
const validador = document.querySelector(".validador");
3+
const Resultado = document.querySelector(".accessResult");
4+
const imagen = document.querySelector(".centerSide img");
5+
6+
// Checks everytime someone clicks the button
7+
validador.addEventListener("click", function() {
8+
let error = true;
9+
10+
if(creditCardValue.value.length === 0)
11+
Resultado.innerText = "The field is empty!";
12+
else if(creditCardValue.value.length < 16)
13+
Resultado.innerText = "The credit card must contain 16 digit";
14+
else if(creditCardValue.value.charAt(15) % 2 !== 0)
15+
Resultado.innerText = "The last digit of the credit card must be even";
16+
else if(greaterThanSixteen(creditCardValue.value) === false)
17+
Resultado.innerText = "The sum of the digits must be over 16";
18+
else if(atLeastTwoValues(creditCardValue.value) === false)
19+
Resultado.innerText = "The credit card must contain at least two different digits";
20+
else if(isNaN(creditCardValue.value))
21+
Resultado.innerText = "The credit card must contain number digits only";
22+
else {
23+
Resultado.innerText = "Credit Card succesfully validated!";
24+
error = false;
25+
}
26+
27+
if(error === true)
28+
imagen.src = "images/xxxxx.png";
29+
else
30+
imagen.src = "images/tick.png";
31+
});
32+
33+
// Checks everytime someone type a value in the textfield
34+
creditCardValue.addEventListener("input", function() {
35+
36+
creditCardValue.value = creditCardValue.value.trim(); // Eliminate spaces
37+
if(creditCardValue.value.length > 16){
38+
creditCardValue.value = creditCardValue.value.slice(0, 16); // don't allow the user to type more than 16 numbers
39+
}
40+
41+
if(isNaN(creditCardValue.value))
42+
Resultado.innerText = "Warning! There is a character that is not a number!";
43+
else
44+
Resultado.innerText = "";
45+
});
46+
47+
function atLeastTwoValues(number){
48+
let firstValue;
49+
if(number.length > 1) //There must be at least two numbers to compare
50+
firstValue = number.charAt(0);
51+
else
52+
return false;
53+
54+
for(let i=1; i < number.length; i++){
55+
if(firstValue !== number.charAt(i))
56+
return true;
57+
}
58+
return false;
59+
}
60+
61+
function greaterThanSixteen(number){
62+
let sum = 0;
63+
for(let i=0; i<number.length; i++)
64+
sum += parseInt(number.charAt(i));
65+
66+
console.log(sum);
67+
68+
if(sum < 16)
69+
return false;
70+
else
71+
return true;
72+
}
73+
/*
74+
function main(){
75+
console.log(creditCardValue);
76+
}
77+
78+
main();
79+
*/
22.5 KB
Loading
15.1 KB
Loading
17.2 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Write a function named calculateDogAge.
3+
4+
It takes 1 argument: your puppy's age (number).
5+
Calculate your dog's age based on the conversion rate of 1 human year to 7 dog years.
6+
Return a string: "Your doggie is [CALCULATED_VALUE] years old in dog years!"
7+
8+
Call the function three times with different sets of values.
9+
10+
*/
11+
12+
function calculateDogAge(puppyAge){
13+
let puppyAgeInDogAge = puppyAge * 7;
14+
return "Your doggie is " + puppyAgeInDogAge + " years old in dog years!";
15+
}
16+
17+
function main(){
18+
console.log(calculateDogAge(1));
19+
console.log(calculateDogAge(6));
20+
console.log(calculateDogAge(11));
21+
}
22+
23+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Write a function called calculateTotalPrice
3+
4+
It takes 1 argument: an object that contains properties that only contain number values
5+
Add all the number values together
6+
Return a number: the total price of all items
7+
8+
Create an object named cartForParty with 5 properties. Each property should be a grocery item (like beers
9+
or chips) and hold a number value (like 1.75 or 0.99)
10+
Call the function 1 time, giving it the object cartForParty as an argument
11+
*/
12+
let cartForParty = {
13+
beer: 10,
14+
chips: 2.35,
15+
peanuts: 4.50,
16+
milk: 2.67,
17+
apple: 1.23
18+
}
19+
20+
function calculateTotalPrice(itemNumber){
21+
let totalPrice = 0;
22+
23+
for(let key in itemNumber)
24+
totalPrice += itemNumber[key];
25+
return Math.floor(totalPrice * 100) / 100;
26+
}
27+
28+
function main(){
29+
console.log("Total Price: " + calculateTotalPrice(cartForParty));
30+
}
31+
32+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Write a function called calculateTotalPrice
3+
4+
It takes 1 argument: an object that contains properties that only contain number values
5+
Add all the number values together
6+
Return a number: the total price of all items
7+
8+
Create an object named cartForParty with 5 properties. Each property should be a grocery item (like beers
9+
or chips) and hold a number value (like 1.75 or 0.99)
10+
Call the function 1 time, giving it the object cartForParty as an argument
11+
*/
12+
let cartForParty = {
13+
beer: 10,
14+
chips: 2.35,
15+
peanuts: 4.50,
16+
milk: 2.67,
17+
apple: 1.23
18+
}
19+
20+
function calculateTotalPrice(itemNumber){
21+
let totalPrice = 0;
22+
23+
for(let key in itemNumber)
24+
totalPrice += itemNumber[key];
25+
return Math.floor(totalPrice * 100) / 100;
26+
}
27+
28+
function main(){
29+
console.log("Total Price: " + calculateTotalPrice(cartForParty));
30+
}
31+
32+
main();

0 commit comments

Comments
 (0)