1
/*
CS 22A
Assignment 2
Question 2

Write a function snooze that takes on parameter, day, and
returns a boolean: true if day is a weekend, false if otherwise.
If invalid argument/no argument, return false.
The parameter day may be lowercase, uppercase, or mixed.
*/

function snooze(day){
  day = day || 0;

  var Sat = 'Saturday';
  var Sun = 'Sunday';

  if (day === Sat){
    return true;
  } else if (day === Sat.toUpperCase()){
    return true;
  } else if (day === Sat.toLowerCase()){
    return true;
  } else if (day === Sun){
    return true;
  } else if (day === Sun.toUpperCase()){
    return true;
  } else if (day === Sun.toLowerCase()){
    return true;
  } else {
    return false;
  }
}

console.log(snooze('Monday')) // false
console.log(snooze('tuesday')) // false
console.log(snooze('SUNDAY')) // true
console.log(snooze('Saturday')) // true
console.log(snooze('October')) // false
console.log(snooze()) // false
console.log(snooze('SatUrDaY')) // true

Hi all. I'm a bit new to programming and I'm taking up the javascript class at my school at the moment. One of the questions in my assignment this week is to create a function that takes on a parameter and returns a boolean, just as described in the comments above my code.

My issue is that I do not understand how to make it so mixed-case inputs will be understood as their lowercase/uppercase counterparts. I did some googling and most results tell me to use regex, a concept we have yet to cover and I would get in trouble for doing so. Can someone shed some light as to how I can approach this?

Also, while I'm at it- can someone nudge me in the correct path for the last question of this assignment?

Your task is to define a function count that takes two parameters: word and char. The function returns the number of times the character occurs in the given word. You may assume that both parameters are lower case strings.

Again, I have googled how to approach that problem but most results tell me to use regex or arrays, concepts we have yet to cover in class and I would get in trouble for doing so. Any help would be appreciated. Thanks again!

demboiz
  • 141
  • 1
  • 7
  • 3
    You will find time spent in the [documentation for String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) worthwhile. Familiarize yourself with the available methods and you'll be able to approach these tasks with ease in the future. – Marty Apr 21 '15 at 05:21
  • For your last question, one way is to use a for loop with `indexOf`, passing any index >=0 back into the 'fromIndex' parameter. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf Another way is to split on the character like this: `"ababccb".split("b").length-1` – James Wilkins Apr 21 '15 at 05:25
  • Regarding your second problem, if you ignore regex for now, think about how you would manually find out how many times a character appears in line of text. You'd look at each one and say, "Are you the character I'm looking for?" - If yes, add one to a count. If no, move onto the next character. In my mind, that's what they might be after, some basic programming logic to count something. However, if you look at the regex methods, one will "match" the problem you're facing :D – Adrian Lynch Apr 21 '15 at 05:27

5 Answers5

4

Easy, force both to be lowercase.

if (day.toLowerCase() === Sat.toLowerCase()) {
    //... do stuff
}
//...
Norman Breau
  • 2,132
  • 16
  • 35
2

For case insensitive comparison, convert both strings to be compared to the same case.

ekuusela
  • 5,034
  • 1
  • 25
  • 43
2

If you just change your code to day = day.toLowerCase(); and var sat = "saturday"; and then remove all other case conversions, everything will work.

kevintechie
  • 1,441
  • 1
  • 13
  • 15
  • it's such a straight forward fix... can't believe I didn't think of it. Thank you!! – demboiz Apr 21 '15 at 05:26
  • It is a simple fix, but the takeaway from this is, compare like for like. – Adrian Lynch Apr 21 '15 at 05:29
  • 1
    I'd add that the takeaway is to not over think the answer. If your solution (for you studies) starts needing tools you have not yet learned or you're having to handle many permutations of conditions, this is a sure sign that you need to take a step back and see if there is something you can simplify. – kevintechie Apr 21 '15 at 05:39
1

It would need to be something like this:

Question 2

Write a function snooze that takes on parameter, day, and
returns a boolean: true if day is a weekend, false if otherwise.
If invalid argument/no argument, return false.
The parameter day may be lowercase, uppercase, or mixed.
*/

function snooze(day){
  day = (day || "").toString().toLowerCase();

  var Sat = 'saturday';
  var Sun = 'sunday';

  if (day === Sat){
    return true;
  } else if (day === Sun){
    return true;
  } else {
    return false;
  }
}

console.log(snooze('Monday')) // false
console.log(snooze('tuesday')) // false
console.log(snooze('SUNDAY')) // true
console.log(snooze('Saturday')) // true
console.log(snooze('October')) // false
console.log(snooze()) // false
console.log(snooze('SatUrDaY')) // true

For your last question, one way is to use a for loop with indexOf(), passing any index >=0 back into the 'fromIndex' parameter. developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Another way is to split on the character like this: "ababccb".split("b").length-1

James Wilkins
  • 6,836
  • 3
  • 48
  • 73
1

/*
CS 22A
Assignment 2
Question 2

Write a function snooze that takes on parameter, day, and
returns a boolean: true if day is a weekend, false if otherwise.
If invalid argument/no argument, return false.
The parameter day may be lowercase, uppercase, or mixed.
*/

function snooze(day){
  day = ('' + day).toLowerCase();  // Converting day to string

  var Sat = 'saturday';
  var Sun = 'sunday';

  if (day === Sat || day === Sun){
    return true;
  } else {
    return false;
  }
}

console.log(snooze('Monday')) // false
console.log(snooze('tuesday')) // false
console.log(snooze('SUNDAY')) // true
console.log(snooze('Saturday')) // true
console.log(snooze('October')) // false
console.log(snooze()) // false
console.log(snooze('SatUrDaY')) // true

Just do like above. Convert string to lowercase or uppercase so that you don't need to worry if the input format is different

Vigneswaran Marimuthu
  • 2,452
  • 13
  • 16
  • FYI, if day is undefined, you will get the text "undefined" - though it won't really matter in this case I know. – James Wilkins Apr 21 '15 at 05:32
  • @JamesWilkins I was converting to string because i don't want to perform `toLowerCase` operation on non-string parameters. Instead of checking whether the passed parameter is string or not, i converted into string :) – Vigneswaran Marimuthu Apr 21 '15 at 06:00
  • I never questioned that. ;) If I call `snooze()` day would then be converted to the string "undefined", instead of "". Again, not a big deal here though. – James Wilkins Apr 21 '15 at 06:02
  • Using `'' + day` to explicitly cast it to a string is probably overkill for a intro course, the problem as posed seems to assume the input will be a valid string. That `if` statement could also be simplified to a single line: `return day === Sat || day === Sun || false;`. If `day === Sat` is true it will return `true` otherwise `day === Sun` is true it world return `true`, if neither is true it would default to `false`. – Useless Code Apr 21 '15 at 06:03
  • But `day` might be in lowercase or uppercase or even mixed. So this is not going to handle it right – Vigneswaran Marimuthu Apr 21 '15 at 06:08