Last active
December 12, 2023 20:17
-
-
Save desinas/b492dfb90b15acf9de92cf1546d5b27b to your computer and use it in GitHub Desktop.
JavaScript intro to functions Mocha tested by Flatiron School, learn.co
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// describe('shout(string)', function() { | |
// it('receives one argument and returns it in all caps', function() { | |
// expect(shout('hello')).toEqual('HELLO') | |
// }) | |
// }) | |
function shout (aWord) { | |
return aWord.toUpperCase( ); | |
} | |
// describe('whisper(string)', function() { | |
// it('receives one argument and returns it in all lowercase', function() { | |
// expect(whisper('HELLO')).toEqual('hello') | |
// }) | |
// }) | |
function whisper (aWord) { | |
return aWord.toLowerCase( ); | |
} | |
// describe('logShout(string)', function() { | |
// it('calls console.log() its one argument in all caps', function() { | |
// const spy = expect.spyOn(console, 'log').andCallThrough() | |
// logShout('hello') | |
// expect(spy).toHaveBeenCalledWith('HELLO') | |
// console.log.restore() | |
// }) | |
// }) | |
function logShout (aWord) { | |
console.log(aWord.toUpperCase( )); | |
} | |
// describe('logWhisper(string)', function() { | |
// it('calls console.log() its one argument in all lowercase', function() { | |
// const spy = expect.spyOn(console, 'log').andCallThrough() | |
// logWhisper('HELLO') | |
// expect(spy).toHaveBeenCalledWith('hello') | |
// console.log.restore() | |
// }) | |
// }) | |
function logWhisper (aWord) { | |
console.log(aWord.toLowerCase( )); | |
} | |
// describe('sayHiToGrandma(string)', function() { | |
// it('returns "I can\'t hear you!" if `string` is lowercase', function() { | |
// expect(sayHiToGrandma('hello')).toEqual("I can't hear you!") | |
// }) | |
// it('returns "YES INDEED!" if `string` is uppercase', function() { | |
// expect(sayHiToGrandma('HELLO')).toEqual("YES INDEED!") | |
// }) | |
// it('returns "I love you, too." if `string` is "I love you, Grandma."`', function() { | |
// expect(sayHiToGrandma("I love you, Grandma.")).toEqual("I love you, too.") | |
// }) | |
// }) | |
function sayHiToGrandma (aWord) { | |
var cantUnswer = "I can't hear you!"; | |
var yesUnswer = "YES INDEED!"; | |
var lovUnswer = "I love you, too."; | |
if (aWord.toLowerCase(aWord) === aWord) { | |
return cantUnswer; | |
} | |
else if (aWord.toUpperCase(aWord) === aWord) { | |
return yesUnswer; | |
} | |
else if ("I love you, Grandma." === aWord) { | |
return lovUnswer | |
} | |
} |
thought: The Revision 4 is the conditional expressed with the switch with various case situations. It is out of the lesson added version of the code, without the mocha tests to pass.
case aWord.toLowerCase(aWord) === aWord : unswer = "I can't hear you!";
this kind of case is not throwing an error but it is not working. 🐛
ty =)
Note: Although there are 3 tests for sayHiToGrandma(), you only need to write one function. This function should be able to handle all three test conditions:
what is this function?
Thank you for this
It looks like this is your version of how you came up with?
It does work and I just altered it a little so i can understand more, and again it works thank you!
THANK YOU THANK YOU!!!
THANK YOU.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
trick: With this condition
aWord.toLowerCase(aWord) === aWord
it is checked if the string is lowercased. To the left side, the function to lower case the string is called and then it is checked if it is the same with the string before the transformation. 💡