Welcome to the Linux Foundation Forum!

Unable to solve ch-11 > labs-2

'use strict'
const assert = require('assert')
const str = 'buffers are neat'
const base64 = Buffer.from(str, 'base64') // convert str to base64

console.log(base64)

assert.equal(base64, Buffer.from([
89,110,86,109,90,109,86,121,99,
121,66,104,99,109,85,103,98,109,
86,104,100,65,61,61]))

console.log('passed!')

Comments

  • nayib
    nayib Posts: 9

    The challenge state is "Convert a String to base64 Encoded String by Using a Buffer". The result for this challenge is "YnVmZmVycyBhcmUgbmVhdA==" given the string "buffers are neat" . Taking this into account there might be a mistake either in the challenge statement or the assertion .

    Looking forward to getting an answer from @davidmarkclements

  • Im stuck on this one too

  • Hello, I can confirm this pass with 0 errors. If you want the solution =>

    const base64 = Buffer.from(str).toString('base64');

  • @xwarzdev is correct.

  • Thanks to teacher David, and @xwarzdev.
    The string was transfered to base64.

  • jwandekoken
    jwandekoken Posts: 4
    edited August 26

    When i implement this answer that xwardev pointed, i still could not manage it to pass:

    "use strict";
    const assert = require("assert");
    const str = "buffers are neat";
    
    const base64 = Buffer.from(str).toString("base64");
    console.log("base64: ", base64);
    
    assert.strictEqual(
      base64,
      Buffer.from([
        89, 110, 86, 109, 90, 109, 86, 121, 99, 121, 66, 104, 99, 109, 85, 103, 98,
        109, 86, 104, 100, 65, 61, 61,
      ])
    );
    
    console.log("passed!");
    

    The output I am getting:

    base64:  YnVmZmVycyBhcmUgbmVhdA==
    node:assert:123
      throw new AssertionError(obj);
      ^
    
    AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
    + actual - expected
    
    + 'YnVmZmVycyBhcmUgbmVhdA=='
    - Buffer(24) [Uint8Array] [
    -   89,
    -   110,
    -   86,
    -   109,
    -   90,
    -   109,
    -   86,
    -   121,
    -   99,
    -   121,
    -   66,
    -   104,
    -   99,
    -   109,
    -   85,
    -   103,
    -   98,
    -   109,
    -   86,
    -   104,
    -   100,
    -   65,
    -   61,
    -   61
    - ]
        at Object.<anonymous> (/home/jwandekoken/coding/nodejs_JSNAD-labs/ch-11/labs-2/index.js:8:8)
        at Module._compile (node:internal/modules/cjs/loader:1095:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
        at Module.load (node:internal/modules/cjs/loader:975:32)
        at Function.Module._load (node:internal/modules/cjs/loader:816:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
        at node:internal/main/run_main_module:17:47 {
      generatedMessage: true,
      code: 'ERR_ASSERTION',
      actual: 'YnVmZmVycyBhcmUgbmVhdA==',
      expected: Buffer(24) [Uint8Array] [
         89, 110,  86, 109,  90, 109,  86,
        121,  99, 121,  66, 104,  99, 109,
         85, 103,  98, 109,  86, 104, 100,
         65,  61,  61
      ],
      operator: 'strictEqual'
    }
    
  • gregsheppard
    gregsheppard Posts: 24
    edited August 28

    @jwandekoken, you're using assert.strictEqual rather than assert.equal, which is what I see in the course code and coerces the buffer to a string (or maybe the other way around?). Try either switching to assert.equal or adding the toString() method to the buffer.

  • @gregsheppard said:
    @jwandekoken, you're using assert.strictEqual rather than assert.equal, which is what I see in the course code and coerces the buffer to a string (or maybe the other way around?). Try either switching to assert.equal or adding the toString() method to the buffer.

    Thanks for the reply, @gregsheppard. It was indeed that. It worked. I am using strictEqual because it says on my editor that assert.equal is being deprecated, heheh.

  • Fantastic @jwandekoken! Thanks for helping me clarify the difference between the two assert methods myself. I hadn't properly wrapped my head around it until I looked into this after your post.

This discussion has been closed.

Categories

Upcoming Training