Home > other >  Trying to reverse order of four verses in javascript
Trying to reverse order of four verses in javascript

Time:11-26

I´m trying to write the necessary statements to print the four verses of the Programmer's Lament but in reverse order. I´m trying to learn more about code and doing some exercises, the verses are ones below:

I really hate this damned machine

I wish that they would sell it.

It never does quite what I want

But only what I tell it.

They say that I have to use console.log for each one of the lines. I already try several ways but none is correct.

These lines were the closest I got, but only the last line changed, as it gives me an error when creating another string

console.log('I really hate this damned machine');
console.log('It never does quite what I want');
console.log('I wish that they would sell it');
console.log('But only what I tell it');

let string = "I really hate this damned machine It never does quite what I want I wish that they would sell it But only what I tell it";

let reversed = [...string].reverse().join("");
console.log(reversed);

Can anyone help me with this? Maybe it is easy but I´m a junior on this :)

Thanks

CodePudding user response:

If starting with a punctuated string, split it on the ". " delimiter to get an array of each sentence. reverse the array of sentences, then join to get back to a string.

let string = "I really hate this damned machine. It never does quite what I want. I wish that they would sell it. But only what I tell it";
let array = string.split(". ");
let reversed = array.reverse().join(".\n");
console.log(reversed   '.');
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

  • Start with a multiline string provided maybe as Template literal like ...

    `I really hate this damned machine
    I wish that they would sell it.
    It never does quite what I want
    But only what I tell it.`
  • split multiline string with the help of a Regular Expression at any new line (\n) into an array.

  • reverse the array.

  • join the reversed array again ... of cause with new line / \n.

`I really hate this damned machine
I wish that they would sell it.
It never does quite what I want
But only what I tell it.`.split(/\n/).reverse().join('\n');

// - use backticks which enclose a multiline string.
//   - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals]
const lament = `I really hate this damned machine
I wish that they would sell it.
It never does quite what I want
But only what I tell it.`;

// log to prove it is a multiline string.
console.log({ lament });

// - split mulitline string with the help of a
//   regular expression at any new line (`\n`)
//   into an array.
//   - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split]
//   - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp]
//   - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#using_regular_expression_to_split_lines_with_different_line_endingsends_of_lineline_breaks]
console.log(
  'lament.split(/\\n/) ...',

  lament.split(/\n/)
);

// - reverse the array
//   - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse]
console.log(
  'lament.split(/\\n/).reverse() ...',

  lament.split(/\n/).reverse()
);

// - join the reversed array
//   - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join]
console.log(
  'lament.split(/\\n/).reverse().join(\'\\n\') ...\n',

  lament.split(/\n/).reverse().join('\n')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

In response to your words

but I´m a junior on this :)

I'll say you're quite smart. You've efficiently used the spread operator. Building further on this

Splitting the problem into the following

  1. How to have a multiline text in Javascript
  2. Split the lines
  3. Reverse the lines
  4. Join them back

How to have a multiline text? You can have it using backticks

Template strings

`I really hate this damned machine

I wish that they would sell it.

It never does quite what I want

But only what I tell it.`

// reversing the lines individually
/* we can create one function called 
reverseLine to make the code easier to read
*/

function reverseLine(line){

  return [...line].reverse().join("")
}

let verse = `I really hate this damned machine

I wish that they would sell it.

It never does quite what I want

But only what I tell it.`

// splitting the lines based on the new line. this provides an array

let lines = verse.split("\n")

// using a technique called map
//refer doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

let reversedLines = lines.map(reverseLine).join("\n")

console.log(reversedLines)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related