[英]Odin project's task to make a function 'capitalize' and I have no idea if my answer is any good?
So I've been doing the Odin Project and I'm at functions right now and there's a task "Write a function called capitalize that takes a string and returns that string with only the first letter capitalized. Make sure that it can take strings that are lowercase, UPPERCASE or BoTh."所以我一直在做 Odin 项目,我现在正在做函数,有一个任务“写一个 function 调用大写,它接受一个字符串并返回该字符串,只有第一个字母大写。确保它可以接受字符串是小写、大写或两者兼而有之。”
<script> function capitalize(myName, myNameUpper, myNameRan) { const toUpper = "Bartek"; myName = "bartek"; myNameUpper = "BARTEK"; myNameRan = "BaRtEk"; myName, myNameUpper, myNameRan = toUpper; console.log(myName, myNameUpper, myNameRan) } capitalize(); </script>
Is my function any good?我的function好用吗? Thanks in advance for help.
在此先感谢您的帮助。
<script>
function capitalize(lowercase,uppercase,both ) {
const capitalizeLower = lowercase.charAt(0).toUpperCase() + lowercase.slice(1).toLowerCase();
const capitalizeUpper = uppercase.charAt(0).toUpperCase() + uppercase.slice(1).toLowerCase();
const capitalizeBoth = both.charAt(0).toUpperCase() + both.slice(1).toLowerCase();
console.log(capitalizeLower, capitalizeUpper , capitalizeBoth)
return `${capitalizeLower} ${capitalizeUpper} ${capitalizeBoth}`
}
capitalize("hello","HELLO","hEllO");
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.