Skip to content

Commit 422dedd

Browse files
truncate a string javascript program
1 parent fb26601 commit 422dedd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

TruncateAString.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!--
2+
3+
freeCodeTask:
4+
Truncate a string (first argument) if it is longer than the given maximum string length
5+
(second argument). Return the truncated string with a ... ending.
6+
7+
-->
8+
9+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
<meta charset="UTF-8">
13+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
14+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
15+
<title>Truncate a string</title>
16+
</head>
17+
<body>
18+
19+
</body>
20+
<script>
21+
function truncateString(str,num){
22+
if(str.length > num){
23+
return str.slice(0, num) + "...";
24+
}else {
25+
return str
26+
}
27+
}
28+
29+
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8))
30+
</script>
31+
</html>

0 commit comments

Comments
 (0)