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
118 changes: 118 additions & 0 deletions 01week/datatypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

// 1. Write a JavaScript program to display the current day and time.
function displayDateAndTime() {
return Date();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice simple return!

}

console.log('\n\ndisplayDateAndTime');
console.log('------------------');
console.log('The current date and time is: ', displayDateAndTime());



// 2. Write a JavaScript program to convert a number to a string.
function convertNumberToString(num) {
// ensure num is really a number, to handle strings, nulls, etc

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would you do if num isn't a number?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirement reads "convert a number to a string" and does not define how exceptional cases should be handled, so I chose to handle arguments that are "not a number" by returning the string 'NaN'. This is accomplished by first casting the incoming value using Number(). Invalid values, such as null, undefined, objects, etc, will produce NaN, which then becomes 'NaN' when toString() is called.

const convertedNum = Number(num);
return convertedNum.toString();
}

console.log('\n\nconvertNumberToString');
console.log('---------------------');
console.log("5 should convert to '5': ", convertNumberToString(5) === '5');
console.log("5.5 should convert to '5.5': ", convertNumberToString(5.5) === '5.5');
console.log("5.5 should convert to '5.5': ", convertNumberToString(5.5) === '5.5');
console.log("'five' should convert to 'NaN': ", convertNumberToString() === 'NaN');
console.log("null should convert to '0': ", convertNumberToString(null) === '0');



// 3. Write a JavaScript program to convert a string to the number.
function convertStringToNumber(str) {
return Number(str);
}

console.log('\n\nconvertStringToNumber');
console.log('---------------------');
console.log("'5' should convert to 5: ", convertStringToNumber('5') === 5);
console.log("'5.5' should convert to 5.5: ", convertStringToNumber('5.5') === 5.5);
console.log("'five' should convert to NaN: ", Number.isNaN(convertStringToNumber('five')));
console.log("'' should convert to 0: ", convertStringToNumber('') === 0);



// 4. Write a JavaScript program that takes in different datatypes and prints out whether they are a:
// Boolean
// Null
// Undefined
// Number
// NaN
// String
function getDataType(val) {
// NaN and null are special cases
if (Number.isNaN(val)) return 'NaN';
if (val === null) return 'null';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice check!


return typeof val;
}

console.log('\n\ngetDataType');
console.log('-----------');
console.log("5 should return 'number': ", getDataType(5) === 'number');
console.log("'5' should return 'string': ", getDataType('5') === 'string');
console.log("true should return 'boolean': ", getDataType(true) === 'boolean');
console.log("null should return 'null': ", getDataType(null) === 'null');
console.log("undefined should return 'undefined': ", getDataType() === 'undefined');
console.log("NaN should return 'NaN': ", getDataType(NaN) === 'NaN');



// 5. Write a JavaScript program that adds 2 numbers together.
function sumOfTwoNumbers(num1, num2) {
return num1 + num2;
}

console.log('\n\nsumOfTwoNumbers');
console.log('--------------');
console.log('2 + 2 should equal 4: ', sumOfTwoNumbers(2, 2) === 4);



// 6. Write a JavaScript program that runs only when 2 things are true.
function printSomethingWhenBothArgsAreTrue(arg1, arg2) {
if (arg1 && arg2) {
console.log('Both arguments are true!');
}
}

console.log('\n\printSomethingWhenBothArgsAreTrue');
console.log('--------------------------------');
printSomethingWhenBothArgsAreTrue(true, false); // should print nothing
printSomethingWhenBothArgsAreTrue(1, 2); // should print 'both args are true'

// 7. Write a JavaScript program that runs when 1 of 2 things are true.
function printSomethingWhenOneArgIsTrue(arg1, arg2) {
if (arg1 || arg2) {
console.log('At least one argument is true!');
}
}

console.log('\n\printSomethingWhenOneArgIsTrue');
console.log('-----------------------------');
printSomethingWhenOneArgIsTrue(true, false); // should print msg
printSomethingWhenOneArgIsTrue(0, null); // should print nothing



// 8. Write a JavaScript program that runs when both things are not true.
function printSomethingWhenBothArgsAreFalse(arg1, arg2) {
if (!(arg1 || arg2)) {
console.log('Neither argument is true!');
}
}

console.log('\n\printSomethingWhenBothArgsAreFalse');
console.log('---------------------------------');
printSomethingWhenBothArgsAreFalse(true, 0); // should not print msg
printSomethingWhenBothArgsAreFalse(null, undefined); // should print msg
24 changes: 19 additions & 5 deletions 01week/rockPaperScissors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ const rl = readline.createInterface({
output: process.stdout
});

// a map that defines what beats what. ie, rock beats
// scissors, paper beats rock, etc.
const beats = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper",
};

function rockPaperScissors(hand1, hand2) {

// Write code here

hand1 = hand1.toLowerCase().trim();
hand2 = hand2.toLowerCase().trim();
// check for tie
if (hand1 === hand2) {
return "It's a tie!";
}
// look up what hand1 beats and see if it is hand2.
if (beats[hand1] === hand2) {
return "Hand one wins!"
}
// if not, hand 2 wins
return "Hand two wins!"
}

function getPrompt() {
Expand All @@ -24,9 +40,7 @@ function getPrompt() {
}

// Tests

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

describe('#rockPaperScissors()', () => {
it('should detect a tie', () => {
assert.equal(rockPaperScissors('rock', 'rock'), "It's a tie!");
Expand Down