Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.3 KiB

title
title
Declare a Read-Only Variable with the const Keyword

Declare a Read-Only Variable with the const Keyword


Problem Explanation

Change all the variables to let or const and rename sentence.


Hints

Hint 1

  • Replace var for string with read-only const.

Hint 1

  • Replace var in for loop to let.

Hint 1

  • Common convention is to name const variables with ALL CAPS.

Solutions

Solution 1 (Click to Show/Hide)
function printManyTimes(str) {
  "use strict";
  const SENTENCE = str + " is cool!";
  for (let i = 0; i < str.length; i += 2) {
    console.log(SENTENCE);
  }
}
printManyTimes("freeCodeCamp");

Code Explanation

By using const on sentence we can make it read-only and by using let on i inside the for loop we can avoid using var all together. For added code clarity we can also change sentence to SENTENCE to show that it is a constant.