js将foo-bar转为fooBar

时间:2022-06-06 08:22:45

这是一道js的面试题,就是考察将普通面命转为小驼峰命名

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body> </body>
<script type="text/javascript">
function fooBar(str) {
var arr = [];
let sting = str.split('-');
arr.push(sting[0]);
for (var i = 1; i < sting.length; i ++) {
arr.push(sting[i].toString()[0].toUpperCase() + sting[i].toString().slice(1));
}
return arr.join('');
}
document.write(fooBar('foo-bar'));
</script>
</html>