Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 60 additions & 0 deletions 01week/datatypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 1. Write a JavaScript program to display the current day and time.
const dateTime = new Date();
document.write(dateTime);

// 2. Write a JavaScript program to convert a number to a string.
var num = 10;
var str = num.toString();
console.log(str)

// 3. Write a JavaScript program to convert a string to the number.
var str = "10"
var num = Number(str)
console.log(typeof num)

// 4. Write a JavaScript program that takes in different datatypes and prints out whether they are a: Boolean, Null, Undefined, Number, NaN, String
console.log(typeof true);
console.log(typeof null);
console.log(typeof x);
console.log(typeof 5);
console.log(typeof Nan)
console.log(typeof "Hello World")

// 5. Write a JavaScript program that adds 2 numbers together.
var x = 5;
var y = 5;
var z = x + y;
console.log(z)

// 6. Write a JavaScript program that runs only when 2 things are true.
function myFunction(val1, val2){
if ((val1 % 2 == 1) && (val2 % 2 == 0)){
return true
} else {
return false
}
}
console.log(myFunction(1, 2))

// 7. Write a JavaScript program that runs when 1 of 2 things are true.
function myFunction(val1, val2){
if ((val1 % 2 == 0) || (val2 % 2 == 1)){
return true
} else {
return false
}
}
console.log(myFunction(4, 2))

// 8. Write a JavaScript program that runs when both things are not true.
function myFunction(val1, val2){
if ((val1 % 2 == 1) && (val2 % 2 == 1)){
return false
} else {
return true
}
}
console.log(myFunction(4, 2))



31 changes: 30 additions & 1 deletion 01week/rockPaperScissors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,36 @@ const rl = readline.createInterface({

function rockPaperScissors(hand1, hand2) {

// Write code here
const player1 = hand1.toLowerCase();
const player2 = hand2.toLowerCase();

if ( player1 === player2 ) {
return "It's a tie!";
}

if ( player1 === "rock" ){
if (player2 === "scissors") {
return "Hand one wins!";
} else if ( player2 === "paper" ) {
return "Hand two wins!";
}
}

if ( player1 === "paper" ){
if (player2 === "scissors") {
return "Hand two wins!";
} else if ( player2 === "rock" ) {
return "Hand one wins!";
}
}

if ( player1 === "scissors" ){
if (player2 === "paper") {
return "Hand one wins!";
} else if ( player2 === "rock" ) {
return "Hand two wins!";
}
}

}

Expand Down
29 changes: 24 additions & 5 deletions 02week/pigLatin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'use wordict';

const assert = require('assert');
const readline = require('readline');
Expand All @@ -8,20 +8,39 @@ const rl = readline.createInterface({
});


function pigLatin(word) {

// Your code here
function pigLatin(word){
function checkVowels(element){
let vowels = ['a', 'e', 'i','o','u']
let passed = false;
for(let v of vowels){
v === element ? passed = true : null;
}
return passed;
}

}
let vowelIndex = word.split('').findIndex(checkVowels) // returns the index of the first vowel

let pigged = word;

// if first letter is a vowel
vowelIndex === 0 ? pigged = word + 'way' :
// if the first two letters are consonants
vowelIndex > 1 || vowelIndex == -1 ? pigged = word.slice(2) + word.slice(0, 2) + 'ay' :
// if the first letter is a consonant and the second letter is a vowel
vowelIndex === 1 ? pigged = word.slice(1) + word[0] + 'ay' : null


return pigged;

}

function getPrompt() {
rl.question('word ', (answer) => {
console.log( pigLatin(answer) );
getPrompt();
});
}

// Tests

if (typeof describe === 'function') {
Expand Down
99 changes: 99 additions & 0 deletions 02week/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});


function rockPaperScissors(hand1, hand2) {

const player1 = hand1.toLowerCase();
const player2 = hand2.toLowerCase();
const validAnswers = "rockpaperscissors";

if ((validAnswers.indexOf(player1) == 1) || (validAnswers.indexOf(player2) == -1)) {
return "Invalid answer(s)";
}

if ( player1 === player2 ) {
return "It's a tie!";
}

if ( player1 === "rock" ){
if (player2 === "scissors") {
return "Hand one wins!";
} else if ( player2 === "paper" ) {
return "Hand two wins!";
}
}

if ( player1 === "paper" ){
if (player2 === "scissors") {
return "Hand two wins!";
} else if ( player2 === "rock" ) {
return "Hand one wins!";
}
}

if ( player1 === "scissors" ){
if (player2 === "paper") {
return "Hand one wins!";
} else if ( player2 === "rock" ) {
return "Hand two wins!";
}
}

}

function getPrompt() {
rl.question('hand1: ', (answer1) => {
rl.question('hand2: ', (answer2) => {
console.log( rockPaperScissors(answer1, answer2) );
getPrompt();
});
});
}

// Tests

if (typeof describe === 'function') {

describe('#rockPaperScissors()', () => {
it('should detect a tie', () => {
assert.equal(rockPaperScissors('rock', 'rock'), "It's a tie!");
assert.equal(rockPaperScissors('paper', 'paper'), "It's a tie!");
assert.equal(rockPaperScissors('scissors', 'scissors'), "It's a tie!");
});
it('should detect which hand won', () => {
assert.equal(rockPaperScissors('rock', 'scissors'), "Hand one wins!");
assert.equal(rockPaperScissors('rock', 'paper' ), "Hand two wins!");

assert.equal(rockPaperScissors('paper', 'rock' ), "Hand one wins!");
assert.equal(rockPaperScissors('paper', 'scissors'), "Hand two wins!");

assert.equal(rockPaperScissors('scissors', 'paper' ), "Hand one wins!");
assert.equal(rockPaperScissors('scissors', 'rock' ), "Hand two wins!");

});
it('should scrub input to ensure lowercase with "trim"ed whitepace', () => {
assert.equal(rockPaperScissors('rOcK', ' paper '), "Hand two wins!");
assert.equal(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!");
assert.equal(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!");
assert.equal(rockPaperScissors('RoCk', 'PaPeR' ), "Hand two wins!");
assert.equal(rockPaperScissors('', '' ), "Hand _ wins!");
});
it('should check for valid entry'), () => {
assert.equal(rockPaperScissors('roq','payper'), "Invalid answer(s)" )
assert.equal(rockPaperScissors('rawk','paeper'), "Invalid answer(s)" )
assert.equal(rockPaperScissors('rick','peaper'), "Invalid answer(s)" )
assert.equal(rockPaperScissors('rack','pappper'), "Invalid answer(s)" )
}
});
} else {

getPrompt();

}