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

大家好。我对编程有点陌生,目前我正在学校学习 javascript 课程。本周我的作业中的一个问题是创建一个接受参数并返回布尔值的函数,正如我的代码上面的注释中所述。

我的问题是我不明白如何制作它,因此混合大小写输入将被理解为它们的小写/大写对应项。我做了一些谷歌搜索,大多数结果告诉我使用正则表达式,这是一个我们尚未涵盖的概念,这样做我会遇到麻烦。有人可以阐明我如何解决这个问题吗?

另外,当我在做这件事的时候——有人可以把我推到正确的道路上来解决这个作业的最后一个问题吗?

你的任务是定义一个函数计数,它接受两个参数:word 和 char。该函数返回字符在给定单词中出现的次数。您可以假设这两个参数都是小写字符串。

再一次,我用谷歌搜索了如何解决这个问题,但大多数结果告诉我使用正则表达式或数组,我们还没有在课堂上涉及的概念,这样做我会遇到麻烦。任何帮助,将不胜感激。再次感谢!

4

5 回答 5

4

简单,强制两者都是小写。

if (day.toLowerCase() === Sat.toLowerCase()) {
    //... do stuff
}
//...
于 2015-04-21T05:19:02.840 回答
2

对于不区分大小写的比较,将要比较的两个字符串转换为相同的大小写。

于 2015-04-21T05:18:52.630 回答
2

如果您只是将代码更改为day = day.toLowerCase();然后var sat = "saturday";删除所有其他大小写转换,一切都会正常工作。

于 2015-04-21T05:21:47.267 回答
1

它需要是这样的:

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 循环indexOf(),将任何索引 >=0 传递回“fromIndex”参数。developer.mozilla.org/en/docs/Web/JavaScript/Reference/… 另一种方法是像这样分割字符:"ababccb".split("b").length-1

于 2015-04-21T05:21:00.370 回答
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

就像上面一样。将字符串转换为小写或大写,这样您就不必担心输入格式是否不同

于 2015-04-21T05:21:31.273 回答