First init
This commit is contained in:
36
_small_exercices/calculate_weight.js
Normal file
36
_small_exercices/calculate_weight.js
Normal file
@@ -0,0 +1,36 @@
|
||||
// Write your function here:
|
||||
|
||||
function calculateWeight(earthWeight, planet) {
|
||||
if (!Number.isInteger(earthWeight)) {
|
||||
return("Please enter an integer for weight.");
|
||||
}
|
||||
if (typeof planet !== "string") {
|
||||
return("Please enter a string for planet.");
|
||||
}
|
||||
|
||||
switch (planet) {
|
||||
case "Mercury" :
|
||||
return earthWeight * 0.378;
|
||||
case "Venus" :
|
||||
return earthWeight * 0.907;
|
||||
case "Mars" :
|
||||
return earthWeight * 0.377;
|
||||
case "Jupiter" :
|
||||
return earthWeight * 2.36;
|
||||
case "Saturn" :
|
||||
return earthWeight * 0.916;
|
||||
default :
|
||||
return("Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.");
|
||||
}
|
||||
}
|
||||
|
||||
console.log(calculateWeight(100, "Camembert"));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Uncomment the line below when you're ready to try out your function
|
||||
// console.log(calculateWeight(100, 'Jupiter')) // Should print 236
|
||||
|
||||
// We encourage you to add more function calls of your own to test your code!
|
||||
24
_small_exercices/race_day.js
Normal file
24
_small_exercices/race_day.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const getRunner = (runners) => {
|
||||
let runner = 0;
|
||||
|
||||
while (runner < runners) {
|
||||
let raceNumber = Math.floor(Math.random() * 1000);
|
||||
let isRegistered = Math.random() < 0.5; // get 50% chance of getting true
|
||||
let runnerAge = Math.floor(Math.random() * 45 + 15); // considering a minimum age of 15yo and maximum age of 60yo
|
||||
|
||||
if (isRegistered && runnerAge > 18) {
|
||||
raceNumber += 1000;
|
||||
console.log(`Runner ${raceNumber} are to begin race at 09:30 MT.`);
|
||||
} else if (!isRegistered && runnerAge > 18) {
|
||||
raceNumber += 1000;
|
||||
console.log(`Runner ${raceNumber} are to begin race at 11:00 MT. Please register before 10:00 MT.`);
|
||||
} else if (runnerAge < 18) {
|
||||
console.log(`Runner ${raceNumber} are to begin race at 12:30 MT. Please register before 11:30 MT.`);
|
||||
} else {
|
||||
console.log(`Please come to the registration desk for clarification.`);
|
||||
}
|
||||
runner++;
|
||||
}
|
||||
}
|
||||
|
||||
getRunner(10);
|
||||
69
_small_exercices/rock_paper_scissors.js
Normal file
69
_small_exercices/rock_paper_scissors.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const getComputerChoice = () => {
|
||||
const randomValue = Math.floor(Math.random() * 3);
|
||||
switch (randomValue) {
|
||||
case 0 :
|
||||
return "rock";
|
||||
case 1 :
|
||||
return "paper";
|
||||
case 2 :
|
||||
return "scissors";
|
||||
}
|
||||
}
|
||||
|
||||
const getUserChoice = (userInput) => {
|
||||
userInput ? userInput = userInput.toLowerCase() : userInput = getComputerChoice();
|
||||
switch (userInput) {
|
||||
case "rock" :
|
||||
case "paper" :
|
||||
case "scissors" :
|
||||
case "bomb" :
|
||||
return userInput;
|
||||
default :
|
||||
console.log('Error: Invalid input. Please choose rock, paper, scissors, or bomb.');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const determineWinner = (userChoice, computerChoice) => {
|
||||
if (userChoice === computerChoice) {
|
||||
return(`${userChoice} against ${computerChoice}. Tie.`);
|
||||
} else if (userChoice === "bomb") {
|
||||
return(`${userChoice} against ${computerChoice}. The bomb exploded, everything but the user lose.`);
|
||||
} else if (userChoice === "rock") {
|
||||
if (computerChoice === "paper") {
|
||||
return(`${userChoice} against ${computerChoice}. Computer wins.`);
|
||||
} else {
|
||||
return(`${userChoice} against ${computerChoice}. User wins.`);
|
||||
}
|
||||
} else if (userChoice === "paper") {
|
||||
if (computerChoice === "scissors") {
|
||||
return(`${userChoice} against ${computerChoice}. Computer wins.`);
|
||||
} else {
|
||||
return(`${userChoice} against ${computerChoice}. User wins.`);
|
||||
}
|
||||
} else if (userChoice === "scissors") {
|
||||
if (computerChoice === "rock") {
|
||||
return(`${userChoice} against ${computerChoice}. Computer wins.`);
|
||||
} else {
|
||||
return(`${userChoice} against ${computerChoice}. User wins.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*console.log(determineWinner("rock", "scissors"));
|
||||
console.log(determineWinner("rock", "paper"));
|
||||
console.log(determineWinner("bomb", "rock"));*/
|
||||
|
||||
const playGame = (choice) => {
|
||||
const userChoice = getUserChoice(choice);
|
||||
const computerChoice = getComputerChoice();
|
||||
|
||||
console.log(`
|
||||
User choose > ${userChoice}
|
||||
Computer choose > ${computerChoice}
|
||||
------------------------------`);
|
||||
|
||||
console.log(determineWinner(userChoice, computerChoice));
|
||||
}
|
||||
|
||||
playGame(); // type "bomb" to cheat
|
||||
Reference in New Issue
Block a user