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!')

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • Posts: 12

    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 =>

    Spoiler

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

  • @xwarzdev is correct.

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

  • Posts: 4
    edited August 2021

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

    Spoiler
    1. "use strict";
    2. const assert = require("assert");
    3. const str = "buffers are neat";
    4.  
    5. const base64 = Buffer.from(str).toString("base64");
    6. console.log("base64: ", base64);
    7.  
    8. assert.strictEqual(
    9. base64,
    10. Buffer.from([
    11. 89, 110, 86, 109, 90, 109, 86, 121, 99, 121, 66, 104, 99, 109, 85, 103, 98,
    12. 109, 86, 104, 100, 65, 61, 61,
    13. ])
    14. );
    15.  
    16. console.log("passed!");

    The output I am getting:

    Spoiler
    1. base64: YnVmZmVycyBhcmUgbmVhdA==
    2. node:assert:123
    3. throw new AssertionError(obj);
    4. ^
    5.  
    6. AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
    7. + actual - expected
    8.  
    9. + 'YnVmZmVycyBhcmUgbmVhdA=='
    10. - Buffer(24) [Uint8Array] [
    11. - 89,
    12. - 110,
    13. - 86,
    14. - 109,
    15. - 90,
    16. - 109,
    17. - 86,
    18. - 121,
    19. - 99,
    20. - 121,
    21. - 66,
    22. - 104,
    23. - 99,
    24. - 109,
    25. - 85,
    26. - 103,
    27. - 98,
    28. - 109,
    29. - 86,
    30. - 104,
    31. - 100,
    32. - 65,
    33. - 61,
    34. - 61
    35. - ]
    36. at Object.<anonymous> (/home/jwandekoken/coding/nodejs_JSNAD-labs/ch-11/labs-2/index.js:8:8)
    37. at Module._compile (node:internal/modules/cjs/loader:1095:14)
    38. at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    39. at Module.load (node:internal/modules/cjs/loader:975:32)
    40. at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    41. at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    42. at node:internal/main/run_main_module:17:47 {
    43. generatedMessage: true,
    44. code: 'ERR_ASSERTION',
    45. actual: 'YnVmZmVycyBhcmUgbmVhdA==',
    46. expected: Buffer(24) [Uint8Array] [
    47. 89, 110, 86, 109, 90, 109, 86,
    48. 121, 99, 121, 66, 104, 99, 109,
    49. 85, 103, 98, 109, 86, 104, 100,
    50. 65, 61, 61
    51. ],
    52. operator: 'strictEqual'
    53. }
  • Posts: 31
    edited August 2021

    @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.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training