0% found this document useful (0 votes)
9 views2 pages

Functions Exercise

Uploaded by

yubanmoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Functions Exercise

Uploaded by

yubanmoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

function difference(x, y) {

return x-y
}

function product(x, y) {
return x*y
}

function printDay(dayNum) {
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"]
if (dayNum <= 7 && dayNum >= 1) {
return days[dayNum-1];
}
}

function lastElement(arr) {
return arr[arr.length-1];
}

function numberCompare(x, y) {
if (x===y) {
return "Numbers are equal";
}
else if (x>y) {
return "First is greater";
}
else {
return "Second is greater";
}
}

function singleLetterCount(word, letter) {


let count=0
for (let i= 0; i < word.length; i++){
if(word[i].toLowerCase() === letter.toLowerCase()) {
count++;
}
}
return count;
}

function multipleLetterCount(str){
str = str.toLowerCase();
let finalObj = {};
for(let i =0; i< str.length; i++){
if (finalObj[str[i]] === undefined){
finalObj[str[i]] = 1;
} else {
finalObj[str[i]]++;
}
}
return finalObj;
}
function arrayManipulation(arr, com, loc, val){
if (com === ("remove") && loc === ("end")) {
outPut = arr.pop();
}
else if (com === ("remove") && loc === ("beginning")) {
outPut = arr.shift();
}
else if (com === ("add") && loc === ("beginning")) {
arr.unshift(val);
outPut = arr;
}
else if (com === ("add") && loc === ("end")) {
arr.push(val);
outPut = arr;
}
return outPut;
}

function isPalindrome(str){
str = str.toLowerCase().replaceAll(" ", "");
if (str === str.split("").reverse().join("")) {
return true;
}
return false;
}

function rpsGame() {
const choices = ['Rock', 'Paper', 'Scissors'];
const rules = {
Rock: { wins: 'Scissors' },
Paper: { wins: 'Rock' },
Scissors: { wins: 'Paper' }
};
const playerChoice = prompt('Choose Rock, Paper, or Scissors');
const houseChoice = choices[Math.floor(Math.random() * choices.length)];
const result = `You picked ${playerChoice} and the house picked ${houseChoice}!
`;
if (!choices.includes(playerChoice))
return "INVALID CHOICE"
else if (playerChoice === houseChoice) {
return `TIE! ${result}`;
}
else if (rules[playerChoice].wins === houseChoice) {
return `WIN! ${result}`;
}
return `LOSE! ${result}`;
}

You might also like