lobjc
lobjc

Reputation: 2961

JavaScript syntax error with text newlines

When running the html inspect on the console an error comes up when some text is picked up containing new lines. I have tried correcting it in CSS with white-space: pre-line, but the error persists. Below is the full code without the error:

<script type="text/javascript" async="">
        (function () {
    var Message;
    Message = function (arg) {
        this.text = arg.text, this.message_side = arg.message_side;
        this.draw = function (_this) {
            return function () {
                var $message;
                $message = $($('.message_template').clone().html());
                $message.addClass(_this.message_side).find('.text').html(_this.text);
                $('.messages').append($message);
                return setTimeout(function () {
                    return $message.addClass('appeared');
                }, 0);
            };
        }(this);
        return this;
    };
    $(function () {
        var getMessageText, message_side, sendMessage;
        message_side = 'left';
        getMessageText = function () {
            var $message_input;
            $message_input = $('.message_input');
            return $message_input.val();
        };
        sendMessage = function (text) {
            var $messages, message;
            if (text.trim() === '') {
                return;
            }
            $('.message_input').val('');
            $messages = $('.messages');
            message_side = message_side === 'left' ? 'right' : 'left';
            message = new Message({
                text: text,
                message_side: message_side
            });
            message.draw();
            return $messages.animate({ scrollTop: $messages.prop('scrollHeight') }, 300);
        };
        $('.send_message').click(function (e) {
            return sendMessage(getMessageText());
        });
        $('.message_input').keyup(function (e) {
            if (e.which === 13) {
                return sendMessage(getMessageText());
            }
        });
        sendMessage("metabolism blah blah blah");
        setTimeout(function () {
            return sendMessage("Metabolism is the sum total of all of enzyme-catalyzed reactions in the cells. Exergonic reactions result in energy release whereas endergonic reactions result in stored or absorbed energy. Heat is continually being produced in the body as a product of metabolism. As heat is produced, it is also continuously being lost to the environment. Metabolism could be in the form of carbohydrate, lipid or protein metabolism. Aerobic or oxidative, energy system is highly engaged when adequate oxygen is available. The majority of energy derived from aerobic metabolism comes from the oxidation of CHOs and fats. Energy is defined as the ability to perform work, and this changes in proportion to the magnitude of work done. Basal metabolic rate (BMR) is the minimal level of energy needed to sustain bodily functions. The higher the body mass ( larger the body size), the higher the BMR. Lean body mass (LBM; or fat-free mass) is a strong component of BMR. This is why resistance training(RT) is important to increasing BMR and reducing body fat.  ");
        }, 2000);
    });
}.call(this));
    </script>

And below is part of the code with the error. How can i make sure that the text remains inside "" in the code:

return sendMessage("Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilo calories per day.
For males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years).
For females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years).
We also use correction factors for physical activity levels as follows:
1) Sedentary: RMR x 1. 4.
2) Moderately active: RMR x 1. 6.
3) Highly active: RMR x 1. 8. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 ml/minute of oxygen. Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilocalories per day. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 mL/minute of oxygen. Total energy expenditure or BMR is a total of resting metabolic rate (RMR), comprising of up to 60%75% of the total, the thermogenic effect of physical activity taking up to 15%30%, and thermic effect of food consumption and digestion comprising of 10%. ");
        }, 2000);

Upvotes: 0

Views: 73

Answers (3)

connexo
connexo

Reputation: 56853

Simply use ES6 template literals which were designed (amongst other things) to solve exactly that.

return sendMessage(`Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilo calories per day.
For males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years).

For females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years).
We also use correction factors for physical activity levels as follows:
1) Sedentary: RMR x 1. 4.
2) Moderately active: RMR x 1. 6.
3) Highly active: RMR x 1. 8. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 ml/minute of oxygen. Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilocalories per day. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 mL/minute of oxygen. Total energy expenditure or BMR is a total of resting metabolic rate (RMR), comprising of up to 60%75% of the total, the thermogenic effect of physical activity taking up to 15%30%, and thermic effect of food consumption and digestion comprising of 10%.`);

Upvotes: 2

dkulkarni
dkulkarni

Reputation: 2830

You will need to concatenate your strings.

return sendMessage("Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilo calories per day."
+ "\nFor males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years)."
+ "\nFor females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years)."
+ "\nWe also use correction factors for physical activity levels as follows:");

Upvotes: 0

distanteagle16
distanteagle16

Reputation: 1

You need to escape new line in strings defined with single or double quotes. At the end of each line, put a backslash, and as you're going to render it as HTML, put a <br> tag before:

return sendMessage("Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilo calories per day.<br>\
For males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years).<br>\
For females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years).<br>\
We also use correction factors for physical activity levels as follows:<br>\
1) Sedentary: RMR x 1. 4.<br>\
2) Moderately active: RMR x 1. 6.<br>\
3) Highly active: RMR x 1. 8. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 ml/minute of oxygen. Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilocalories per day. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 mL/minute of oxygen. Total energy expenditure or BMR is a total of resting metabolic rate (RMR), comprising of up to 60%75% of the total, the thermogenic effect of physical activity taking up to 15%30%, and thermic effect of food consumption and digestion comprising of 10%. ");
        }, 2000);

Upvotes: 0

Related Questions