最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

偶数应返回大写

IT培训 admin 6浏览 0评论

偶数应返回大写

在String上创建一个函数,该函数将整数n作为参数。 如果n是偶数,它应该以大写形式返回字符串。 如果n是奇数,它应该以小写形式返回字符串。

基本上,我需要编写一个脚本来实现以下功能。

'stringname'.functionName(5)应该返回'stringname' 'stringname'.functionName(4)应该返回'STRINGNAME'

var i=2;
module.exports = function(i) {
     return String(stringname)["to"+(i%2?"Low":"Upp")+"erCase"]();    
}

但不幸的是我无法实现我想要的输出。

回答如下:

String.prototype.upperIfEven = function(n){
   return (n % 2 == 0) ? this.toUpperCase() : this.toLowerCase();
}

var odd = "Hello".upperIfEven(5)

var even = "Hello".upperIfEven(6)

console.log("Odd -> ",odd)

console.log("Even -> ",even)

偶数应返回大写

在String上创建一个函数,该函数将整数n作为参数。 如果n是偶数,它应该以大写形式返回字符串。 如果n是奇数,它应该以小写形式返回字符串。

基本上,我需要编写一个脚本来实现以下功能。

'stringname'.functionName(5)应该返回'stringname' 'stringname'.functionName(4)应该返回'STRINGNAME'

var i=2;
module.exports = function(i) {
     return String(stringname)["to"+(i%2?"Low":"Upp")+"erCase"]();    
}

但不幸的是我无法实现我想要的输出。

回答如下:

String.prototype.upperIfEven = function(n){
   return (n % 2 == 0) ? this.toUpperCase() : this.toLowerCase();
}

var odd = "Hello".upperIfEven(5)

var even = "Hello".upperIfEven(6)

console.log("Odd -> ",odd)

console.log("Even -> ",even)

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论